我尝试使用以下代码将文件保存到云存储。
bucket_name = app_identity.get_default_gcs_bucket_name()
uploaded_file = self.request.POST.get('uploaded_file')
file_name = getattr(uploaded_file, 'filename', None)
file_content = getattr(uploaded_file, 'file', None)
real_path = ''
if file_name and file_content:
content_t = mimetypes.guess_type(file_name)[0]
real_path = os.path.join('/', bucket_name, user.user_id(), file_name)
with cloudstorage.open(real_path, 'w', content_type=content_t) as f:
f.write(file_content.read())
此代码在我部署时非常有效,但在我的本地计算机上却没有。我收到以下错误消息。
ValueError: Path should have format /bucket/filename but got /app_default_bucket\185804764220139124118\test.pdf
答案 0 :(得分:1)
您正在使用os.path.join()
来操纵本地路径以外的其他内容。
试试这个:
real_path = '/' + bucket_name + '/' + user.user_id()+ '/' +file_name
或者您可以使用posixpath
:
import posixpath
posixpath.join('/', bucket_name, user.user_id(), file_name)
参考:https://docs.python.org/2/library/os.path.html#module-os.path - “os.path module
可用于本地路径。”