使用公共权限上传Amazon S3
我正在使用Amazon C#SDK并尝试上传文件,但默认情况下它具有受限制的权限。 我想公开发布,但我似乎无法找到如何在上传过程中执行此操作。
我的存储桶是公开的,但是当我使用下面的代码上传新文件时,我上传的文件不公开。
以前有人不得不这样做吗?
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; } }
Tahbaza的解决方案是正确的,但它在AWS SDK(2.1 +)的更高版本中不起作用……
考虑在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); }
找到它,需要使用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; } }
我以前和C#中都这样做过,但是你的库没有(所以这可能是有限的帮助)。
我们的想法是发送ACL标头和文件。
这是一种可以指向正确方向的方法。
这里还有一些指向相关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); }
考虑CannedACL方法,如下面的代码段所示:
string filePath = @"PathDesert.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);
您可以使用Amazon.S3.Model.PutObjectRequest的CannedACL属性将文件上传到S3,如下所示,
上述就是C#学习教程:使用公共权限上传Amazon S3分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
var putObjectRequest = new Amazon.S3.Model.PutObjectRequest { BucketName = bucketName, Key = key, CannedACL = S3CannedACL.PublicReadWrite };
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1006903.html