如何确保 boost::文件系统::remove 不会尝试删除由其他进程使用的文件
How to be sure that boost::filesystem::remove does not try to delete a file that is used by another process?
我想将文件上传到 AWS S3 存储桶。在执行此操作之前,我从中创建.gz文件以减少其存储。上传后,我想再次使用 boost::文件系统::删除删除.gz文件。
似乎上传正在阻止它正在上传的文件,我无法找到等待完整结果的方法,因此文件不再被锁定。
删除与 main() 中的不同调用一起工作。上传调用后,它立即不起作用,并且 boost operations.hpp 会引发异常。
异常表示该文件已被另一个进程使用。
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(第 664 行):
inline
// For standardization, if the committee doesn't like "remove", consider
"eliminate"
bool remove(const path& p) {return detail::remove(p);}
有没有办法确保如果文件不再被阻止,该文件将被删除?
当您尝试删除该文件时,您的 Aws::FStream 仍处于打开状态。 因此,您只需要在尝试删除它之前关闭它。
我想你可以简单地打电话
input_data->close();
在删除文件之前。
相关文章:
- 如何删除包含其他向量的向量?
- 当动态库中的指针仍被其他库使用时,如何删除该指针
- 将一个字符替换为字符串中更多数量的字符,而不删除C++中的其他字母
- 删除提升interprocess_mutex,当其他进程可能正在使用它时
- QPointer::clear() 是删除其引用的指针,还是"Clears this QPointer object."其他含义?
- 删除其他类的类驱动器
- 为什么删除的复制构造函数不允许使用其他具有多态类型的构造函数?
- 向前删除标题文件中其他名称空间的自定义类型
- 我应该删除来自其他函数或类方法的指针吗?
- C++:从动态结构数组中删除元素并移动其他元素
- 如何确保 boost::文件系统::remove 不会尝试删除由其他进程使用的文件
- 有没有办法将擦除-删除成语与其他循环条件一起使用
- 预编译标头 - 从所有其他文件中删除所有预编译的标题所必需的
- 是否能够从容器中删除其对象的其他引用
- 程序删除其他用户的文件
- 程序删除其他用户的文件 - 权限被拒绝
- remove()函数会删除其他所有文件
- 如果我删除其他进程的共享内存,会发生什么
- 从BOOST或c++ 14类中删除其他单例类中的ACE_Singleton
- 迭代映射并调用删除其他元素的函数就是映射