简体   繁体   中英

Error in uploading file on S3 when bucket name containing periods(dots) through c# SDK

I have created two bucket on S3, named like "demobucket" and "demo.bucket". When I am uploading any file on "demobucket" it works fine. But when I upload file on "demo.bucket", it gives me an error "Maximum number of retry attempts reached : 3"

My concern is that what is the problem in uploading file when bucket name contain periods(dots).

My code is:

public static bool UploadResumeFileToS3(string uploadAsFileName, Stream ImageStream, S3CannedACL filePermission, S3StorageClass storageType)
    {
        try
        {
            AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(MY_AWS_ACCESS_KEY_ID, MY_AWS_SECRET_KEY);

PutObjectRequest request = new PutObjectRequest();
            request.WithKey(uploadAsFileName);
            request.WithInputStream(ImageStream);
            request.WithBucketName("demo.bucket");
            request.CannedACL = filePermission;
            request.StorageClass = storageType;

            client.PutObject(request);
            client.Dispose();
        }
        catch
        {
            return false;
        }
        return true;
    }

There is a problem establishing a secure connection to S3 when the bucket name contains a period. The issue is explained well here: http://shlomoswidler.com/2009/08/amazon-s3-gotcha-using-virtual-host.html .

One solution is to create your S3 client passing a third argument which causes it to use HTTP instead of HTTPS. See Amazon S3 C# SDK "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel." Error .

    AmazonS3Config S3Config = new AmazonS3Config()
    {
        ServiceURL = "s3.amazonaws.com",
        CommunicationProtocol = Protocol.HTTP,
        RegionEndpoint = region
    };

However, be aware that this is not secure and Amazon does not recommend it. Your secret access key could possibly be intercepted. I have not yet found a secure way to upload a file to a bucket with a period in the name.

If you are using a recent version of AWSSDK.dll (at least 2.0.13.0), you can do this instead:

    AmazonS3Config S3Config = new AmazonS3Config()
    {
        ServiceURL = "s3.amazonaws.com",
        ForcePathStyle = true,
        RegionEndpoint = region
    };

This forces the S3Client to use the version of the path to your bucket which avoids the problem.

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