c# - 从 Amazon s3 流中读取文本

标签 c# amazon-s3 streamreader readline readfile

我使用以下代码从 Amazon S3 读取文本文件,并逐行处理它。此代码有效,但问题是速度很慢。

 GetObjectRequest getObjRequest = new GetObjectRequest()
    .WithBucketName(amazonSettings.BucketName)
    .WithKey(_fileKey);
using (AmazonS3 client = AWSClientFactory.CreateAmazonS3Client(
    amazonSettings.AccessKey, 
    amazonSettings.SecretAccessKey))
using (GetObjectResponse getObjRespone = client.GetObject(getObjRequest))
using (Stream amazonStream = getObjRespone.ResponseStream)
{                        
    StreamReader amazonStreamReader = new StreamReader(amazonStream);
    tempGsContact = new GSContact();
    while ((_fileLine = amazonStreamReader.ReadLine()) != null)
    {
        if (_fileLine.Equals("END:VCARD"))
        {
            // Make process 1
        }
        else if (!_fileLine.Equals(string.Empty))
        {
            //Make process 2
        }
    }                        
}

问题是:我能否获得更充分的方法来降低时间成本?

最佳答案

.NET中的HTTPWebResponse也有类似的性能瓶颈,这可能是他们做的AmazonS3类正在包装的。

这是由对象花费很长时间解析代理设置引起的,列出了一些可能的解决方案 here ,但最简单的选择可能是将以下内容添加到您的 app.config 文件中:

<system.net>
  <defaultProxy enabled="false">
    <proxy/>
    <bypasslist/>
    <module/>
  </defaultProxy>
</system.net>

或者,您可以在此处替换调用:

AWSClientFactory.CreateAmazonS3Client(amazonSettings.AccessKey, amazonSettings.SecretAccessKey)

通过调用 overload它接受“AmazonS3Config”的第三个参数,您可以在其中通过“AmazonS3Config.ProxyHost = null”指定一个空代理 - 这实际上应该与上述仅针对该请求的配置更改相同。

关于c# - 从 Amazon s3 流中读取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15052390/

相关文章:

c# - 当await类型为Task的变量时发生了什么

c# - 如何从后面的 c# 代码调用 javascript 函数

amazon-web-services - 将文件复制到 S3 中的文件夹中,最终位于父文件夹中

amazon-web-services - 如何在不安装 AWS SDK 的情况下通过 Powershell 从 S3 下载文件?

node.js - 从 s3 文件中读取范围之间的行

c# - 无法从 C :\Testing\Docs using C#.Net 读取 txt 文件

c# - 如何使用已作为 IQurable 查询的 ToListAsync 变量来 Moq 设置等待?

c# - 使用 lambda 返回连接的字符串

c# - StreamReader、ThreadSafety 和读取模式的问题