使用谷歌应用引擎发送图像到谷歌云存储 [英] Sending images to google cloud storage using google app engine
问题描述
我用python中的google app引擎编写了一个简单的webapp,允许用户上传图片并将其存储在某处(现在我只是使用基于教程的blob商店)。
现在我想将图像发送到谷歌云存储,但不知道如何做到这一点。打开文件时提供两种模式:a和r。就我所知,它们都不是用于二进制流的。
如何将图像发送到谷歌云存储?代码片段或引用会很好。
我打算发送小音频样本以及其他二进制数据。
这是一个简单的上传处理程序,它将写入上传的blob到大店。请注意,您需要将应用的服务帐户添加到管理大型存储桶的团队帐户。
class UploadHandler(blobstore_handlers .BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file')
blob_info = upload_files [0]
in_file_name = files.blobstore.get_file_name( blob_info.key())
infile = files.open(in_file_name)
out_file_name ='/ gs / mybucket /'+ blob_info.filename
outfile = files.gs.create(out_file_name,
mime_type = blob_info.content_type)
$ bf = files.open(outfile,'a')
chunkSize = 1024 * 1024
尝试:
data = infile .read(chunkSize)
while data:
f.write(data)
data = infile.read(chunkSize)
finally:
infile.close()
f.close()
files.finalize(outfile)
I wrote a simple webapp using google app engine in python that allows users to upload images and have it stored somewhere (for now I'm just using the blob store based on the tutorials).
Now I want to send the images to google cloud storage, but am not sure how to do this. They provide two modes when opening a file: "a" and "r". Neither of them, to my knowledge, are for binary streams.
How can I send an image to google cloud storage? Code snippets or references would be nice. I am planning to send small audio samples as well, and other binary data.
Also, how can I delete the image if a user wishes to delete it? There doesn't seem to be a delete method available.
Here's a simple upload handler that will write the blob that was uploaded to bigstore. Note you will need to have added your app's service account to the team account that is managing the bigstore bucket.
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file')
blob_info = upload_files[0]
in_file_name = files.blobstore.get_file_name(blob_info.key())
infile = files.open(in_file_name)
out_file_name = '/gs/mybucket/' + blob_info.filename
outfile = files.gs.create(out_file_name,
mime_type = blob_info.content_type)
f = files.open(outfile, 'a')
chunkSize = 1024 * 1024
try:
data = infile.read(chunkSize)
while data:
f.write(data)
data = infile.read(chunkSize)
finally:
infile.close()
f.close()
files.finalize(outfile)
这篇关于使用谷歌应用引擎发送图像到谷歌云存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!