0
votes

I want to upload a file to AWS S3 bucket. Before doing this I create a .gz file from it to reduce its storage. After uploading it I want to delete the .gz file again with boost::filesystem::remove.

It seems like that the upload is blocking the file it is uploading and I wasn't able to find a way to wait for the complete result so the file wouldn't be locked anymore.

The removing works with a different call in the main(). Directly after the uploading call it does not work and boost operations.hpp throws an exception.

The exception says that the file is already used by another process.

int main(int argc, char *argv[])
{
boost::filesystem::path path{ C:/Development/test.gz };

//deletes the file correctly if called
//boost::filesystem::remove(path); 

//upload file first, then delete it
putObject(path)
}

void putObject(path, s3client)
{
auto input_data = Aws::MakeShared<Aws::FStream>("", path.string(),
std::ios_base::in | std::ios_base::binary);

auto request = Aws::S3::Model::PutObjectRequest();
request.WithBucket("bucketName").WithKey(key);
request.SetBody(input_data);
request.SetMetadata(metadata);

    //upload file to s3 bucket
s3client->PutObject(request);

//throws exception if called - file is in use
boost::filesystem::remove(path);

}

boost operations.hpp that throws the exception (line 664):

inline
// For standardization, if the committee doesn't like "remove", consider  
"eliminate"
bool remove(const path& p)           {return detail::remove(p);}

Is there a way to be sure that the file will be deleted if it isn't blocked anymore?

1
It's not an AWS problem, seems like this is a local file? On Windows? As long as it's used, you cannot remove it, and there is no simple way to check if a file is in use or not.Matthieu Brucher

1 Answers

1
votes

When you try to delete the file, your Aws::FStream is still open. So you just have to close it before you try to delete it.

I think you can simply call

input_data->close();

before deleting the file.