简体   繁体   English

使用公共权限上传Amazon S3

[英]Amazon S3 upload with public permissions

I'm using the Amazon C# SDK and trying to upload a file, but by default it has restricted permissions. 我正在使用Amazon C#SDK并尝试上传文件,但默认情况下它具有受限制的权限。 I would like to make it publicly available, but I can't seem to find out how to do it as part of the upload. 我想公开发布它,但我似乎无法找到如何在上传过程中执行此操作。

My bucket is public, but when I upload a new file using the code below, the file I upload is not public. 我的存储桶是公共的,但是当我使用下面的代码上传新文件时,我上传的文件不公开。

Has anyone had to do this before? 以前有人不得不这样做吗?

public class S3Uploader
{
    private string awsAccessKeyId;
    private string awsSecretAccessKey;
    private string bucketName;
    private Amazon.S3.Transfer.TransferUtility transferUtility;

    public S3Uploader(string bucketName)
    {
        this.bucketName = bucketName;
        this.transferUtility = new Amazon.S3.Transfer.TransferUtility("XXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

    }

    public void UploadFile(string filePath, string toPath)
    {
        AsyncCallback callback = new AsyncCallback(uploadComplete);
        transferUtility.BeginUpload(filePath, bucketName, toPath, callback, null);
    }

    private void uploadComplete(IAsyncResult result)
    { 
        var x = result;
    }
}

The solution by Tahbaza is correct, but it doesn't work in the later versions of AWS SDK (2.1+)... Tahbaza的解决方案是正确的,但它在AWS SDK(2.1 +)的更高版本中不起作用......

Consider using the following for 2.1+ versions of SDK: 考虑在2.1+版本的SDK中使用以下内容:

private void UploadFileToS3(string filePath)
{
    var awsAccessKey = ConfigurationManager.AppSettings["AWSAccessKey"];
    var awsSecretKey = ConfigurationManager.AppSettings["AWSSecretKey"];
    var existingBucketName = ConfigurationManager.AppSettings["AWSBucketName"];
    var client = Amazon.AWSClientFactory.CreateAmazonS3Client(awsAccessKey, awsSecretKey,RegionEndpoint.USEast1);

    var uploadRequest = new TransferUtilityUploadRequest
    {
        FilePath = filePath,
        BucketName = existingBucketName,
        CannedACL = S3CannedACL.PublicRead
    };

var fileTransferUtility = new TransferUtility(client);
    fileTransferUtility.Upload(uploadRequest);
} 

Found it, need to use a TransferUtilityUploadRequest : 找到它,需要使用TransferUtilityUploadRequest

public class S3Uploader
{
    private string awsAccessKeyId;
    private string awsSecretAccessKey;
    private string bucketName;
    private Amazon.S3.Transfer.TransferUtility transferUtility;

    public S3Uploader(string bucketName)
    {
        this.bucketName = bucketName;
        this.transferUtility = new Amazon.S3.Transfer.TransferUtility("XXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

    }

    public void UploadFile(string filePath, string toPath)
    {
        AsyncCallback callback = new AsyncCallback(uploadComplete);
        var uploadRequest = new TransferUtilityUploadRequest();
        uploadRequest.FilePath = filePath;
        uploadRequest.BucketName = "my_s3_bucket";
        uploadRequest.Key = toPath;
        uploadRequest.AddHeader("x-amz-acl", "public-read");
        transferUtility.BeginUpload(uploadRequest, callback, null);
    }

    private void uploadComplete(IAsyncResult result)
    { 
        var x = result;
    }
}

I have done this before, and in C#, but not with your library (so this may be of limited help). 我以前在C#中已经完成了这个,但是你的库没有(所以这可能是有限的帮助)。

The idea is to send an ACL header along with the file. 我们的想法是发送ACL标头和文件。

This is one way to do it that should point you in the right direction. 这是一种可以指向正确方向的方法。

Here's also a link to some relevant AWS docs. 这里还有一些指向相关AWS文档的链接

    private const string AWS_ACL_HEADER = "x-amz-acl";

    private static string ToACLString(S3ACLType acl) {
        switch (acl) {
            case S3ACLType.AuthenticatedRead:
                return "authenticated-read";
            case S3ACLType.BucketOwnerFullControl:
                return "bucket-owner-full-control";
            case S3ACLType.BucketOwnerRead:
                return "bucket-owner-read";
            case S3ACLType.Private:
                return "private";
            case S3ACLType.PublicRead:
                return "public-read";
            case S3ACLType.PublicReadWrite:
                return "public-read-write";
            default: return "";
        }
    }

    public void Put(string bucketName, string id, byte[] bytes, string contentType, S3ACLType acl) {
        string uri = String.Format("https://{0}/{1}", BASE_SERVICE_URL, bucketName.ToLower());
        DreamMessage msg = DreamMessage.Ok(MimeType.BINARY, bytes);
        msg.Headers[DreamHeaders.CONTENT_TYPE] = contentType;
        msg.Headers[DreamHeaders.EXPECT] = "100-continue";
        msg.Headers[AWS_ACL_HEADER] = ToACLString(acl);
        Plug s3Client = Plug.New(uri).WithPreHandler(S3AuthenticationHeader);
        s3Client.At(id).Put(msg);
    }

Consider CannedACL approach, shown in the follwing snippet: 考虑CannedACL方法,如下面的代码段所示:

        string filePath = @"Path\Desert.jpg";
        Stream input = new FileStream(filePath, FileMode.Open);
        PutObjectRequest request2 = new PutObjectRequest();
        request2.WithMetaData("title", "the title")
            .WithBucketName(bucketName)
            .WithCannedACL(S3CannedACL.PublicRead)
            .WithKey(keyName)
            .WithInputStream(input);

You can use the property CannedACL of Amazon.S3.Model.PutObjectRequest to upload files to S3 as shown below, 您可以使用Amazon.S3.Model.PutObjectRequest的CannedACL属性将文件上传到S3,如下所示,

 var putObjectRequest = new Amazon.S3.Model.PutObjectRequest
                        {
                            BucketName = bucketName,
                            Key = key,
                            CannedACL = S3CannedACL.PublicReadWrite
                        }; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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