簡體   English   中英

使用S3BotoStorage移動文件

[英]Move Files with S3BotoStorage

我正在使用來自django- storages的S3BotoStorage在Django后端的S3存儲桶上處理媒體和靜態文件。

一切都按預期工作,但后端引發一個not ImplementedError,因為當我在Image是models.Image字段的地方調用self.image.path時,它不是本地文件系統。 這是預期的行為

但是,我使用的路徑是使用os.rename移動圖像(它也無法在我的存儲桶中運行)。 將文件移到存儲桶中的方法是什么?

與其使用os來移動文件, os使用存儲

要移動文件,可以在FileField (或在本例中為ImageField )上使用read()方法(並最終使用storage刪除舊文件)。

file_content = my_model.my_image.read()
file_name = my_model.my_image.name

# Renaming the file
my_model.my_image = my_model.my_image.save('new_file_name', ContentFile(file_content))

# If you want to delete the original file afterwards:
from django.core.files.storage import default_storage
default_storage.delete(file_name)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM