Amazon S3 client does not release the file after the upload RRS feed

  • Question

  • I have a wcf service here to upload the files to Amazon s3 server. After the successful upload, I need to delete the file from my local path. But when I try to delete the file, got an error says The process cannot access the file.Because its being used by another process".Sharing below my code snippets.

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

    If anyone know about the issue, please update.

    Thursday, April 2, 2020 11:47 AM

All replies

  • Hi,
    Using object as the input parameter than the Stream may cause this issue, the object resources failed to be released.  On my side, I often upload the file by using Stream. As you know, reading/writing a large file is completed by the file stream. We should pay attention to operate the file stream carefully.
    public async Task UploadStream(Stream stream)
                var context = WebOperationContext.Current;
                string filename = context.IncomingRequest.Headers["filename"].ToString();
                string ext = Path.GetExtension(filename);
                using (stream)
                    //save the image under the Uploads folder on the server-side(root directory).
                    using (var file = File.Create(Path.Combine(HostingEnvironment.MapPath("~/Uploads"), Guid.NewGuid().ToString() + ext)))
                        await stream.CopyToAsync(file);

    Besides, WCF does not support multipart/form data by default, therefore, I upload other file information by using Http header.
    Best Regards
    Abraham Qian

    Friday, April 3, 2020 3:22 AM