加快 Amazon S3 的写入速度

发布于 2024-12-11 18:58:55 字数 680 浏览 0 评论 0 原文

我正在尝试使用 C# 将图像上传到 Amazon S3:

PutObjectRequest titledRequest = null;

S3Response response = null;

using (var memoryStream = new MemoryStream())
{

    image.Save(memoryStream, ImageFormat.Png);

    titledRequest = new PutObjectRequest();

    titledRequest.WithBucketName(bucketName)
                 .WithKey(keyName)
                 .WithCannedACL(S3CannedACL.PublicRead)
                 .WithInputStream(memoryStream);

    response = client.PutObject(titledRequest);
}

如您所见,我没有在本地保存图像文件,而是将其流式传输到 S3。然而,由于某种原因,对于 50kb 的文件,此过程大约需要 50 秒!

我的上传速度远超过 1mbps,没有任何问题。

我想知道先保存文件然后上传会更快吗?

我应该考虑什么来加快上传过程?再说一次,宽带方面没有问题!

I am trying to upload an image to Amazon S3 using C#:

PutObjectRequest titledRequest = null;

S3Response response = null;

using (var memoryStream = new MemoryStream())
{

    image.Save(memoryStream, ImageFormat.Png);

    titledRequest = new PutObjectRequest();

    titledRequest.WithBucketName(bucketName)
                 .WithKey(keyName)
                 .WithCannedACL(S3CannedACL.PublicRead)
                 .WithInputStream(memoryStream);

    response = client.PutObject(titledRequest);
}

As you can see, I am not saving the image file locally but rather streaming it to S3. However, for some reason, this process takes around 50 seconds for a 50kb file!

There is nothing wrong with my upload speed its well over 1mbps.

I'm wondering is it faster to save the file first and upload?

Is there anything I should consider to speed up upload process? Again, no problems on the broadband side of things!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同尘 2024-12-18 18:58:55

亚马逊文档说:

如果从输入流上传,请始终使用
内容大小设置。否则输入流的内容必须
在发送到 Amazon S3 之前先在内存中缓冲。这可能会导致
非常负面的性能影响。

很难相信这可能是您的问题,但您应该遵循的最佳实践。

其他需要考虑的事情 -

  • 是否需要指定其他 ObjectMetadata 来告诉 S3 如何处理
    你的形象?
  • 存储桶是否存在,或者创建是第一个对象的一部分
    发送操作?

Amazon docs say:

If uploading from an input stream, always specify metadata with the
content size set. Otherwise the contents of the input stream have to
be buffered in memory before being sent to Amazon S3. This can cause
very negative performance impacts.

Hard to believe this could be your problem, but a best practice you should follow.

Other things to consider -

  • do you need to specify other ObjectMetadata to tell S3 how to handle
    your image?
  • does the bucket exist, or is creation a part of the first object
    send operation?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文