Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# AmazonS3在上传后不会发布该文件_C#_.net_Amazon S3 - Fatal编程技术网

C# AmazonS3在上传后不会发布该文件

C# AmazonS3在上传后不会发布该文件,c#,.net,amazon-s3,C#,.net,Amazon S3,我有一个wcf服务在这里上传文件到亚马逊s3服务器。成功上传后,我需要从本地路径删除该文件。但是当我尝试删除文件时,出现一个错误,表示进程无法访问该文件。因为它正被另一个进程使用。请在我的代码片段下面共享 var putRequest = new PutObjectRequest { BucketName = System.Configuration.ConfigurationManager.AppSettings["S3Bucket"] .

我有一个wcf服务在这里上传文件到亚马逊s3服务器。成功上传后,我需要从本地路径删除该文件。但是当我尝试删除文件时,出现一个错误,表示进程无法访问该文件。因为它正被另一个进程使用。请在我的代码片段下面共享

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);

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

此处可能存在计时问题,因此您可能希望尝试显式关闭流

请注意,我不确定,如果我弄错了,我会删除这个,但它是渴望一个评论

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);

有关属性的更多信息:

此处可能存在计时问题,因此您可能希望尝试显式关闭流

请注意,我不确定,如果我弄错了,我会删除这个,但它是渴望一个评论

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);

有关属性的更多信息:

如果您使用
块将所有S3代码(删除前)放入
中该怎么办?如果您使用
块将所有S3代码(删除前)放入
中该怎么办?我尝试了您的代码。但未能上载文件,错误显示“访问被拒绝”“真奇怪。。。。文件已经打开了吗?在这种情况下,无法删除它也是有道理的。我尝试使用您的代码。但未能上载文件,错误显示“访问被拒绝”,这很奇怪。。。。文件已经打开了吗?在这种情况下,无法删除它也是有意义的