简体   繁体   English

在 s3 python boto3 中替换文件

[英]Replace file in s3 python boto3

If we have to completely replace an existing file in specified s3 folder path with another file (with different filename) using python (lambda)如果我们必须使用 python (lambda) 将指定 s3 文件夹路径中的existing file完全替换为另一个文件(with different filename)

At any instant of time the particular folder path in s3 would have only one file, which would be completely replaced with another when running the python code在任何时刻,s3 中的特定文件夹路径都只有一个文件,在运行 python 代码时将完全替换为另一个文件

Which boto function could be used for the above.哪个 boto 函数可用于上述目的。 Thanks!谢谢!

To do this you'd simply need to call the upload_object function which actually is a wrapper for the put_object function.为此,您只需调用upload_object函数,该函数实际上是put_object函数的包装器。

The function call would look like the below.函数调用如下所示。

import boto3
s3 = boto3.resource('s3')
s3.meta.client.upload_file('/tmp/hello.txt', 'mybucket', 'replace/this/file.txt')

As long as you do not have object versioning enabled it will replace the previous object that existed for that key before.只要您没有启用对象版本控制,它就会替换之前为该键存在的先前对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM