Skip to content

Instantly share code, notes, and snippets.

@chartjes
Created December 4, 2017 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chartjes/d2a09f35bddbcab124ffc677b90f526b to your computer and use it in GitHub Desktop.
Save chartjes/d2a09f35bddbcab124ffc677b90f526b to your computer and use it in GitHub Desktop.
Question about Flysystem
In OpenCFP we store the generated name of speaker images that people upload
and then assume the path is always '/uploads/<name of file>'
I have plans to want to allow OpenCFP to be deployed on Heroku where we could
not write image files to the filesystem. I have already played around with
Flysystem and have it writing files locally but I want to add the ability to
have those files exist on S3 and the app find them.
Is there a method where I can ask an adapter "what is your path" and then
prepend that to the image name we're storing in the database.
So I could do something like:
<img src="<value for path>/<name of image in database>">
Am I better off serializing indivdual Flysystem objects and storing them
in the database instead?
@frankdejonge
Copy link

Ok, so it's super simple. You're using an S3 bucket as a CDN, which is totally legit (you can even put cloudfront in front of it for better caching/delivery). Flysystem stores everything relative to a root, in AWS S3's case this is a combination of a bucket name and a path prefix. Given you know the prefix, all you have to do is prepend the public URL to it. So actually you don't need to do anything with flysystem for this.

Alternatively you could use a call on the S3Client instance to resolve this automagically which is:

$url = $s3Client->getObjectUrl('bucket-name', 'relative/path/to/file.png');

PS: Remember that Flysystem is a tool to interact with filesystems, while public URLs are part of S3 they're not "filesystem behaviour". So it's more than OK to interact with S3 through flysystem for writing and reading but retrieving public (user accessible) URLs through another method.

@chartjes
Copy link
Author

chartjes commented Dec 4, 2017

Thanks for the clarification Frank!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment