我想从python脚本上传Google Cloud Storage上的图片。这是我的代码:
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient import discovery
scopes = ['https://www.googleapis.com/auth/devstorage.full_control']
credentials = ServiceAccountCredentials.from_json_keyfile_name('serviceAccount.json', scop
es)
service = discovery.build('storage','v1',credentials = credentials)
body = {'name':'my_image.jpg'}
req = service.objects().insert(
bucket='my_bucket', body=body,
media_body=googleapiclient.http.MediaIoBaseUpload(
gcs_image, 'application/octet-stream'))
resp = req.execute()
如果gcs_image = open('img.jpg', 'r')
代码正常工作并将我的图片正确保存在云端存储上。如何直接上传字节图像? (例如,来自OpenCV / Numpy数组:gcs_image = cv2.imread('img.jpg')
)
答案 0 :(得分:2)
如果您想从文件上传图片。
import os
from google.cloud import storage
def upload_file_to_gcs(bucket_name, local_path, local_file_name, target_key):
try:
client = storage.Client()
bucket = client.bucket(bucket_name)
full_file_path = os.path.join(local_path, local_file_name)
bucket.blob(target_key).upload_from_filename(full_file_path)
return bucket.blob(target_key).public_url
except Exception as e:
print(e)
return None
但是如果你想直接上传字节:
import os
from google.cloud import storage
def upload_data_to_gcs(bucket_name, data, target_key):
try:
client = storage.Client()
bucket = client.bucket(bucket_name)
bucket.blob(target_key).upload_from_string(data)
return bucket.blob(target_key).public_url
except Exception as e:
print(e)
return None
请注意target_key
是前缀和上传文件的名称。
答案 1 :(得分:0)
MediaIoBaseUpload
需要一个类似io.Base
的对象并引发以下错误:
'numpy.ndarray' object has no attribute 'seek'
收到ndarray对象后。要解决此问题,我使用TemporaryFile
和numpy.ndarray().tofile()
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient import discovery
import googleapiclient
import numpy as np
import cv2
from tempfile import TemporaryFile
scopes = ['https://www.googleapis.com/auth/devstorage.full_control']
credentials = ServiceAccountCredentials.from_json_keyfile_name('serviceAccount.json', scopes)
service = discovery.build('storage','v1',credentials = credentials)
body = {'name':'my_image.jpg'}
with TemporaryFile() as gcs_image:
cv2.imread('img.jpg').tofile(gcs_image)
req = service.objects().insert(
bucket='my_bucket’, body=body,
media_body=googleapiclient.http.MediaIoBaseUpload(
gcs_image, 'application/octet-stream'))
resp = req.execute()
请注意,googleapiclient是非惯用的,只有维护(它不再开发)。我建议使用idiomatic one。
答案 2 :(得分:0)
就我而言,我想从字节上传PDF文档到Cloud Storage。
当我尝试以下操作时,它创建了一个文本文件,其中包含我的字节字符串。
blob.upload_from_string(bytedata)
为了使用字节串创建实际的PDF文件,我必须要做:
blob.upload_from_string(bytedata, content_type='application/pdf')
我的字节数据是b64编码的,所以我也首先要用b64对其进行解码。