Amazon S3 upload with public permissions

前端 未结 5 1721
再見小時候
再見小時候 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条回答
  •  -上瘾入骨i
    2021-02-01 04:07

    I have done this before, and in C#, but not with your library (so this may be of limited help).

    The idea is to send an ACL header along with the file.

    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.

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

提交回复
热议问题