By pradhvan
I am using the below upload function to upload in a bucket but this does not overwrite the existing file in the bucket. If I upload an image with a particular format and update the image. The overwrite function does not work.
def upload(byte_stream, path, filename):
bucket = doconn.get_bucket()
object_key = os.path.join(path, filename)
params = dict(
Key=object_key,
Body=byte_stream,
ACL='public-read'
)
bucket.put_object(**params)
return object_key
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Heya,
Just came across this answer and decided to write some general guidelines for anyone who comes across this in the future despite the old question.
With DigitalOcean Spaces, if you want to overwrite an existing file, you simply write a new file with the same object key (file path and file name). From the code snippet you provided, it seems that you’re doing this correctly.
There might be some other issue causing this behavior. One possibility is HTTP caching. Most S3 compatible services, including DigitalOcean Spaces, will add generous Cache-Control headers to response objects by default. You might be seeing the old version of the file due to caching, either in your browser or somewhere else along the way.
If you want to avoid this, you can add a Cache-Control ‘no-cache, no-store, must-revalidate’ parameters to your params dictionary.
- params = dict(
- Key=object_key,
- Body=byte_stream,
- ACL='public-read',
- CacheControl='no-cache, no-store, must-revalidate'
- )
Remember, DigitalOcean Spaces is designed to behave similarly to AWS S3, and should overwrite the file with the newer version when you put an object with an existing key.
For more detailed information, you can refer to DigitalOcean Spaces Documentation.
Hope that this helps!
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.