Getting file url after upload amazon s3

14,813

Solution 1

Simply you can generate download expiry link after upload completed.

example:

var expiryUrlRequest = new GetPreSignedUrlRequest()
                           .WithBucketName(BucketName)
                           .WithKey(Key)
                           .WithExpires(DateTime.Now.AddDays(10));

string url = _amazonS3Client.GetPreSignedURL(expiryUrlRequest);

Solution 2

Try this method

GetPreSignedUrlRequest request = new GetPreSignedUrlRequest();
request.BucketName = "my-bucket-name";
request.Key        = "secret_plans.txt";
request.Expires    = DateTime.Now.AddHours(1);
request.Protocol   = Protocol.HTTP;
string url = client.GetPreSignedURL(request);
Console.WriteLine(url);
Share:
14,813

Related videos on Youtube

erkan demir
Author by

erkan demir

Updated on September 18, 2022

Comments

  • erkan demir
    erkan demir over 1 year

    I need to get file url after upload the file to amazons3 server. Here is my upload code. How to return amazons3 path ?

    public static bool UploadToS3(string bucketName, string bucketFilePath, Byte[] localPath)
        {
            var client = Amazon.AWSClientFactory.CreateAmazonS3Client(Config.EmailServer.AwsAccessKey, Config.EmailServer.AwsSecretKey, Amazon.RegionEndpoint.EUWest1);
    
            PutObjectRequest request = new PutObjectRequest()
            {
                BucketName = bucketName,
                Key = bucketFilePath,
                InputStream = new MemoryStream(localPath),
                AutoCloseStream = true,
                CannedACL = S3CannedACL.PublicRead,
                StorageClass = S3StorageClass.ReducedRedundancy                
            };
    
            PutObjectResponse response = client.PutObject(request);
            return true;
        }
    
    • Rico
      Rico over 10 years
      You mean the external URL for your object? You don't really need to get it. It's known: https://<mybucket>.s3.amazonaws.com/mykey . If you want the internal for AWS it's s3://<mybucket>/mykey
  • Ricardo França
    Ricardo França about 8 years
    There is a way to get it from response object?
  • Mike Kellogg
    Mike Kellogg about 8 years
    @ricardofranca There's no need to. The request only requires the bucket name and key, which you should have anyway if you just wrote a file to that bucket name and key.
  • Yahya Younes
    Yahya Younes about 8 years
    @ricardofranca It's depend on the ACL policy of the uploaded object. If you set it to be public(Any body can access it without credential) then you don't need to create PreSignedURL and you can build the URL using the bucket name and the object key.
  • bnns
    bnns almost 6 years
    maximum expiry 604800 secounds.