繁体   English   中英

Amazon S3存储桶C#API未捕获错误

[英]Amazon S3 Bucket C# API Not Catching Errors

对于(可能是)结构性较差的问题表示歉意,因为这是我第一次在此处发布。

我正在使用Amazon的.NET的AWS API将文件保存到存储桶中。 查看其他示例和文档,我想出了以下方法。 该方法成功执行,但是没有文件上传到S3存储桶。 我不确定是否有任何错误,但是没有办法检查,因为Upload()是一个void方法,并且不会引发任何异常。

我不确定是否要提供正确的信息,甚至失败。

有什么方法可以捕获潜在的上载错误或监视上载进度? 还是我做错了什么?

    public static void AddItemToStorage(byte[] byteArray, string itemName)
    {
        MemoryStream itemStream = new MemoryStream(byteArray);

        // sign in information
        var credentials = new Amazon.Runtime.BasicAWSCredentials(
            "APIKey",
            "APIPassword"
            );

       // Link to client
       Amazon.S3.AmazonS3Client client = new Amazon.S3.AmazonS3Client(credentials, Amazon.RegionEndpoint.EUCentral1);

        TransferUtility tu = new TransferUtility(client);

        // Actual file is stored in a GUID folder to make sure there are no collisions with file names
        string bucket = "bucket_name_here/" + Guid.NewGuid().ToString(); 
        string item = itemName.Replace(" ", "_");

        itemStream.Seek(0, SeekOrigin.Begin);
        tu.Upload(itemStream, bucket, item);

        // url would be: http://bucket_here/GUID_Folder/itemName.ext/
    }

问候,

扎克

设法找到一个“解决方案”。 我需要配置为使用HTTP,将ACL设置为PublicRead,并使用上载请求来监视上载。 这仍然没有用,所以我测试了是否通过请求ACL来上传文件。 现在,这使我上传的项目在存储中可见。 仍然不知道为什么,但可能会帮助其他陷入困境的人。

    public static void AddItemToStorage(byte[] byteArray, string itemName)
    {
        MemoryStream itemStream = new MemoryStream(byteArray);

        var config = new AmazonS3Config();
        config.UseHttp = true;
        config.RegionEndpoint = Amazon.RegionEndpoint.EUCentral1;

        var credentials = new Amazon.Runtime.BasicAWSCredentials(
            "AWSUserKey",
            "AWSPassword"
            );

        AmazonS3Client client = new AmazonS3Client(credentials, config);
        TransferUtility tu = new TransferUtility(client);

        itemStream.Seek(0, SeekOrigin.Begin);
        TransferUtilityUploadRequest request = new TransferUtilityUploadRequest()
        {
            BucketName = "bucket_name_here" + Guid.NewGuid().ToString(),
            Key = itemName.Replace(" ", "_"),
            InputStream = itemStream,
            CannedACL = S3CannedACL.PublicRead                
        };

        request.UploadProgressEvent += Request_UploadProgressEvent;

        tu.UploadAsync(request).Wait();

        var resp = client.GetACL(new GetACLRequest()
        {
            BucketName = request.BucketName,
            Key = request.Key
        });

        itemStream.Dispose();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM