【问题标题】:Getting file url after upload amazon s3上传amazon s3后获取文件url
【发布时间】:2014-02-03 15:32:15
【问题描述】:

将文件上传到 amazons3 服务器后,我需要获取文件 url。 这是我的上传代码。 如何返回amazons3路径?

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

【问题讨论】:

  • 什么是“PutObjectRequest”和“PutObjectResponse”?它们是 Amazon S3 库的组件吗?
  • 您的意思是您的对象的外部 URL?你真的不需要得到它。众所周知: https://.s3.amazonaws.com/mykey 。如果您想要 AWS 的内部,它是 s3:///mykey

标签: c# .net amazon-s3 amazon


【解决方案1】:

上传完成后即可生成下载过期链接。

示例:

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

string url = _amazonS3Client.GetPreSignedURL(expiryUrlRequest);

【讨论】:

  • 有办法从响应对象中获取吗?
  • @ricardofranca 没必要。该请求只需要存储桶名称和密钥,如果您只是将文件写入该存储桶名称和密钥,则无论如何都应该拥有。
  • @ricardofranca 这取决于上传对象的 ACL 策略。如果您将其设置为公开(任何主体都可以在没有凭据的情况下访问它),那么您无需创建 PreSignedURL,您可以使用存储桶名称和对象密钥构建 URL。
  • 最长有效期 604800 秒。
【解决方案2】:

试试这个方法

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

【讨论】:

    猜你喜欢
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多