简体   繁体   中英

Amazon S3 Bucket C# API Not Catching Errors

apologies for the (possibly) poorly structured question as this is my first time posting here.

I'm using Amazon's AWS API for .NET to save a file to the bucket. Looking at other examples and the documentation I came up with the method below. The method executes successfully, but no file is uploaded to the S3 bucket. I'm not sure if there's any errors, but there's no way to check as Upload() is a void method and no exceptions are thrown.

I'm not totally sure if I am giving it the right information or if there's even a fail.

Is there any way to capture potential upload errors, or monitor the upload progress? Or is there anything I'm doing wrong?

    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/
    }

Regards,

Zak

Managed to find a "solution". I needed to configure to use HTTP, set the ACL to PublicRead and used an upload request to monitor the upload. This still didn't work, so I tested to see if the file was uploaded by requesting the ACL. This now made my uploaded item visible in the storage. Still no idea why, but might help anyone else who gets stuck on this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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