简体   繁体   中英

Getting file url after upload amazon s3

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;
    }

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);

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);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM