gpt4 book ai didi

c# - 在 C# ASP.NET MVC 5 中将图像上传到 S3

转载 作者:太空宇宙 更新时间:2023-11-03 10:42:02 25 4
gpt4 key购买 nike

我正在尝试将图像上传到我的存储桶,但我不能,因为出现以下错误:

An exception of type 'Amazon.Runtime.AmazonServiceException' occurred in mscorlib.dll but was not handled in user code

详情

{"Encountered a non retryable WebException : RequestCanceled"}

更多细节

{"The request was aborted: The request was canceled."}

内部异常

The stream was already consumed. It cannot be read again.

我正在使用适用于 VS2013 的 AWS 开发工具包。

代码

private const string ExistingBucketName = "******development"; //Name of the bucket
private const string KeyName = "Images";

public static void UploadToS3(string filePath)
{

//filePath -> C:\example.jpg

try
{
var fileTransferUtility = new
TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.USEast1));

// 1. Upload a file, file name is used as the object key name.
fileTransferUtility.Upload(filePath, ExistingBucketName);
Trace.WriteLine("Upload 1 completed");

// 2. Specify object key name explicitly.
fileTransferUtility.Upload(filePath,
ExistingBucketName, KeyName);
Trace.WriteLine("Upload 2 completed");

// 3. Upload data from a type of System.IO.Stream.
using (var fileToUpload =
new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
fileTransferUtility.Upload(fileToUpload,
ExistingBucketName, KeyName);
}
Trace.WriteLine("Upload 3 completed");

// 4.Specify advanced settings/options.
var fileTransferUtilityRequest = new TransferUtilityUploadRequest
{
BucketName = ExistingBucketName,
FilePath = filePath,
StorageClass = S3StorageClass.ReducedRedundancy,
PartSize = 6291456, // 6 MB.
Key = KeyName,
CannedACL = S3CannedACL.PublicRead
};
fileTransferUtilityRequest.Metadata.Add("param1", "Value1");
fileTransferUtilityRequest.Metadata.Add("param2", "Value2");
fileTransferUtility.Upload(fileTransferUtilityRequest);
Trace.WriteLine("Upload 4 completed");
}
catch (AmazonS3Exception s3Exception)
{
Trace.WriteLine(s3Exception.Message);
Trace.WriteLine(s3Exception.InnerException);
}
}

最佳答案

我的错误是:

var fileTransferUtility = new
TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.USEast1));

我使用的区域不正确...我更改为欧洲并且有效。

 var fileTransferUtility = new
TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.EUWest1));

关于c# - 在 C# ASP.NET MVC 5 中将图像上传到 S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24994174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com