I'm developing a C# application in which I make some uploads to AWS S3 service. I'm reading the docs and I can't find where to get the public URL after a upload. Here's my code:
public void AddFile(Stream image, string key)
{
var uploadRequest = new TransferUtilityUploadRequest();
uploadRequest.CannedACL = S3CannedACL.PublicRead;
uploadRequest.InputStream = image;
uploadRequest.BucketName = _bucket;
uploadRequest.Key = key;
using (TransferUtility fileTransferUtility = new TransferUtility(_client))
{
fileTransferUtility.Upload(uploadRequest);
}
}
As you can see I put the access to file as public with S3CannedACL.PublicRead. So it will act like a CDN, I know that amazon has the CloudFront service for CDN servers but I must use the S3 regular bucket.
I want to know if its possible to get the fixed URL after the upload method. I just can see on the tutorials and documentation to get the "PreSignedUrl
" with expire time, but I don't want expire time, I need to save this URL on the database.
Nothing could be easier, just select all files you want to generate URLs for and click the “Web URL” button on the toolbar. You will see the list of URLs for each of the selected files. Click copy to clipboard button and you are ready to paste URLs to other programs such as an HTML files editor.
Amazon Web Services (AWS) S3 objects are private by default. Only the object owner has permission to access these objects. Optionally we can set bucket policy to whitelist some accounts or URLs to access the objects of our S3 bucket.
Your application should be using some configuration/properties file, where the name of the bucket is stored for later reference. Use, for example, a properties file with a properties defined like this:
application.bucket=mybucket-example-com
Use this value both when referencing the bucket in your app (eg replace _bucket
with application.bucket
in your code), and when you want to express or return the public URL.
If your app has uploaded a file to the key userid/images/test.jpg
, then the public URL can be expressed as:
https://[application.bucket].s3.amazonaws.com/[key]
This will resolve to the public URL of
https://mybucket-example-com.s3.amazonaws.com/userid/images/test.jpg
The nice thing about this approach is that in your testing environment, you can have a different bucket specified in your test configuration.
In testing, you may want to use application.bucket=test-mybucket-example-com
.
This way your code would not require any changes in this regard when switching between production and testing environments.
You could actually build this url based on the bucket name and the path where you uploaded the file. See this doc about accessing a bucket, where it describes the url format.
e.g:
https://bucket.s3.amazonaws.com/path/to/your/file
https://bucket.s3-aws-region.amazonaws.com/path/to/your/file
https://s3-aws-region.amazonaws.com/bucket/path/to/your/file
Please note that your file must be public, otherwise you will get a permission denied.
Note: the path-style (https://s3-aws-region.amazonaws.com/bucket/path/to/your/file
is now deprecated and should not be used anymore
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With