【问题标题】:Amazon S3 does not release the file after the upload上传后 Amazon S3 不释放文件
【发布时间】:2020-04-02 19:55:06
【问题描述】:

我在这里有一个 wcf 服务来将文件上传到 Amazon s3 服务器。上传成功后,我需要从本地路径中删除文件。但是当我尝试删除文件时,出现错误说进程无法访问文件。因为它正在被另一个进程使用”。在我的代码 sn-ps 下面分享。

var putRequest = new PutObjectRequest
{
    BucketName = System.Configuration.ConfigurationManager.AppSettings["S3Bucket"]
                       .ToString(),
    Key = keyName,
    FilePath = path,
    ContentType = "application/pdf"
};

client = new AmazonS3Client(bucketRegion);
PutObjectResponse response = await client.PutObjectAsync(putRequest);
putRequest = null;
client.Dispose();  
File.Delete(path);

如果有人知道这个问题,请更新..

【问题讨论】:

  • 如果将所有 S3 代码(删除前)放入 using 块中会怎样?

标签: c# .net amazon-s3


【解决方案1】:

这里可能存在时间问题,因此您可能想尝试显式关闭流。

请注意,我不确定,如果我弄错了,我会删除它,但它很想发表评论。

using (var fileStream = new File.OpenRead(path))
{
    var putRequest = new PutObjectRequest
    {
        BucketName = System.Configuration.ConfigurationManager.AppSettings["S3Bucket"]
                    .ToString(),
        Key = keyName,
        InputStream = fileStream ,
        ContentType = "application/pdf",
        AutoCloseStream = false,
    };

    using (var c = new AmazonS3Client(bucketRegion))
    {
         PutObjectResponse response = await c.PutObjectAsync(putRequest);
    }
} //filestream should be closed here, if not: call fileStream.Close() 

File.Delete(path);

更多房产信息:https://docs.aws.amazon.com/sdkfornet1/latest/apidocs/html/T_Amazon_S3_Model_PutObjectRequest.htm

【讨论】:

  • 我尝试使用您的代码。但无法上传文件,出现错误提示“访问被拒绝”
  • 奇怪....文件已经打开了吗?在这种情况下,无法删除它也是有意义的
猜你喜欢
  • 1970-01-01
  • 2014-02-03
  • 2020-08-14
  • 1970-01-01
  • 1970-01-01
  • 2012-09-28
  • 1970-01-01
  • 1970-01-01
  • 2016-01-10
相关资源
最近更新 更多