Python 从表单将多个图像上载到Google存储
我正在尝试将多张图片(介于Python 从表单将多个图像上载到Google存储,python,flask,google-cloud-platform,Python,Flask,Google Cloud Platform,我正在尝试将多张图片(介于1和40之间的图片和800 kb的图片)上传到我的谷歌云存储中,存储在一个名为shootings的存储桶中。用户提交的表单中包含一个或多个png或jpeg在此处输入代码图像 <form action="{{url_for('picture_storage_upload')}}" method="post" enctype="multipart/form-data"> <label cl
1
和40
之间的图片和800 kb
的图片)上传到我的谷歌云存储中,存储在一个名为shootings
的存储桶中。用户提交的表单中包含一个或多个png
或jpeg在此处输入代码
图像
<form action="{{url_for('picture_storage_upload')}}" method="post" enctype="multipart/form-data">
<label class="labelOne" for="pics">Upload your pictures</label>
<input type="file" name="uploaded_pictures" multiple="multiple" accept="image/x-png, image/jpeg"/>
<input type="submit">
</form>
由于表单返回的是字节,因此我使用的是我的存储类中的push\u from\u string
方法
class Storage:
def __init__(self, credentials, bucket_name):
self.storage_client = storage.Client.from_service_account_json(credentials)
self.bucket_name = bucket_name
self.bucket = None
def push(self, local_file_name, target_file_name):
self.bucket = self.storage_client.get_bucket(self.bucket_name)
blob = self.bucket.blob(target_file_name)
with open(local_file_name, 'rb') as f:
blob.upload_from_file(f)
def push_from_string(self, local_file_name, target_file_name):
self.bucket = self.storage_client.get_bucket(self.bucket_name)
self.bucket.blob(target_file_name).upload_from_string(local_file_name)
return self.bucket.blob(target_file_name).public_url
我仍然收到错误“无法转换为字节”
“您的示例没有显示如何提取文件列表。您的错误没有显示生成它的代码行。编辑您的问题以改进。上传的文件是一个列表。使用request.files.getlist
处理每个文件。
class Storage:
def __init__(self, credentials, bucket_name):
self.storage_client = storage.Client.from_service_account_json(credentials)
self.bucket_name = bucket_name
self.bucket = None
def push(self, local_file_name, target_file_name):
self.bucket = self.storage_client.get_bucket(self.bucket_name)
blob = self.bucket.blob(target_file_name)
with open(local_file_name, 'rb') as f:
blob.upload_from_file(f)
def push_from_string(self, local_file_name, target_file_name):
self.bucket = self.storage_client.get_bucket(self.bucket_name)
self.bucket.blob(target_file_name).upload_from_string(local_file_name)
return self.bucket.blob(target_file_name).public_url