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.
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.
eg:
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
Not sure on the exact command in C#, but in java you can use getResourceUrl() to get the uploaded file url. something similar will also be available in C#.
Below method uploads file in a particular folder in a bucket and return the generated url of the file uploaded.
private String uploadFileToS3Bucket(final String bucketName, final File file) {
final String uniqueFileName = uploadFolder + "/" + file.getName();
LOGGER.info("Uploading file with name= " + uniqueFileName);
final PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, uniqueFileName, file);
PutObjectResult putObjectResult = amazonS3.putObject(putObjectRequest);
return ((AmazonS3Client) amazonS3).getResourceUrl(bucketName, uniqueFileName);
}
As Baig commented, Rodrigo's answer is now outdated.
As per the documentation ( https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-bucket-intro.html ), the public URL is now formed like this: https://bucket-name.s3.Region.amazonaws.com/key-name
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.