Amazon S3 upload with public permissions

前端 未结 5 1821
再見小時候
再見小時候 2021-02-01 03:15

I\'m using the Amazon C# SDK and trying to upload a file, but by default it has restricted permissions. I would like to make it publicly available, but I can\'t seem to find ou

5条回答
  •  情书的邮戳
    2021-02-01 04:03

    Found it, need to use a 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;
        }
    }
    

提交回复
热议问题