Xamarin亚马逊S3上传

h4cxqtbf  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(187)

有没有人有过在Xamarin上使用Amazon S3上传文件的经验?它应该很简单,但是我在使用它时遇到了麻烦。我正在尝试使用https://github.com/xamarin/amazon来上传这样的文件:

try 
{
    var client = new AmazonS3Client ("REDACTED", "REDACTED");
    var transferUtility = new TransferUtility (client);
    TransferUtilityUploadRequest request = new TransferUtilityUploadRequest()
        .WithBucketName("bucketname")
        .WithFilePath(image.LocalPath)
        .WithKey("rodsTest")
        .WithTimeout(5 * 60 * 1000);
    transferUtility.Upload(request);

} catch (Exception ex){
    Console.WriteLine (ex.ToString ());
}

但我遇到了这个异常:
系统对象处理异常:该对象在释放后被使用。在System.Net.WebConnection.BeginWrite(系统.Net.HttpWebRequest请求,系统.Byte[]缓冲区,Int 32偏移量,Int 32大小,系统.AsyncCallback cb,系统.对象状态)[0x 0001 f]位于/Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebConnection.cs:1033在System.Net.WebConnectionStream.BeginWrite(系统.Byte[]缓冲区,Int 32偏移量,Int 32大小,系统.AsyncCallback cb,系统.对象状态)[0x 0026 c]位于/Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebConnectionStream.cs:541
Github repo已经一年没有更新了,所以可能是坏了吧?我想做的就是PUT和DELETE文件,所以我的下一步是用RestSharp来使用REST API,而不是使用 Package 器,但肯定是其他人已经做过了,有人能解释一下吗?

ql3eal8s

ql3eal8s1#

查看:使用xamarin c#上传亚马逊s3文件

AmazonS3Config config = new AmazonS3Config();
config.ServiceURL = serviceUrl; // amazon s3 URL                              
config.UseHttp = true;
config.RegionEndpoint =Amazon.RegionEndpoint.APNortheast1; // your region

AmazonS3Client client = new AmazonS3Client(accessKey,
    secretAccessKey, config);

TransferUtility transferUtility = new TransferUtility(client);

TransferUtilityUploadRequest request = new TransferUtilityUploadRequest();
request.BucketName = s3Bucket;
request.FilePath = filePath; // local file path 

// Test that the path is correct
UIImage image = UIImage.FromFile(filePath);

System.Threading.CancellationToken cancellationToken = new System.Threading.CancellationToken();
await transferUtility.UploadAsync(request, cancellationToken);

相关问题