开发者

Amazon S3 PutObject is very slow

I am placing files into an S3 storage using the below code. I am finding it is exceedingly slow. The stopwatch indicated 18 seconds+. Any suggests or other experiences?

        // upload the file to S3
        AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretAccessKey);

        PutObjectRequest request = new PutObjectRequest();

        FileStream fs = new FileStream(sourceFileName, FileMode.Open);

        reque开发者_高级运维st.WithInputStream(fs);
        request.WithBucketName(bucketName);
        request.WithKey(keyName);
        Stopwatch stp1 = new Stopwatch();
        stp1.Start();
        client.PutObject(request);
        stp1.Stop();
        fs.Close();

This code is C#. I am using the amazon .net sdk.

The file is only 56K in size and my upload bandwidth is 1.87Mbps.


It sounds very similar to a problem I had recently, which was caused by the automatic proxy detection settings in "Internet Options" on Windows.

The Amazon SDK uses WebRequest to make it's HTTP requests and by default WebRequest adheres to the computer's "Internet Option" settings for detecting local proxies. Luckily WebRequest has a static property WebRequest.DefaultWebProxy which, when set to null, removes the automatic proxy detection.

All you need to do is set it to null before you start using AmazonS3:

WebRequest.DefaultWebProxy = null; // here

AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretAccessKey);

[...]

It's worth noting that this static property only needs to be set once per application domain and not every time you want to create an AmazonS3 object.

Alternate approach:

If you don't mind reconfiguring the machine, is to go to:

Windows Control Panel > Internet Options > Connections > Lan Settings

and un-check "Automatically detect settings". If you use this approach, you do not need to set the DefaultWebProxy property at all.

Further info:

When I encountered the issue I asked the following question on SO:

How to turn off the automatic proxy detection in the `AmazonS3` object?

It has more details than my answer here if your interested.


You need to change the BufferSize on the AmazonS3Config

var config = new AmazonS3Config
{
    BufferSize = 65536 // 64KB Use a larger buffer size, normally 8K default.
};


My newest project is built on .Net 6 and the AWSSDK.S3 Nuget package.

Application startup was blazing fast, but first use / instance creation (through injection) took something like 8 to 10 seconds.

What did the trick for me was setting the DefaultConfigurationMode in Program.cs.

var awsOptions = builder.Configuration.GetAWSOptions();
awsOptions.DefaultConfigurationMode = DefaultConfigurationMode.Standard;

[...]

builder.Services.AddDefaultAWSOptions(awsOptions);
builder.Services.AddAWSService<IAmazonS3>();

I hope this helps someone in the future!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜