[英]How to upload a bytes image on Google Cloud Storage from a Python script
我想從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')
)
如果要從文件上傳圖像。
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
是前綴和上載文件的名稱。
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只是非慣用和維護 (它不再開發)。 我建議使用慣用的 。
就我而言,我想從字節上傳PDF文檔到雲存儲。
當我嘗試下面的內容時,它創建了一個帶有字節字符串的文本文件。
blob.upload_from_string(bytedata)
為了使用我必須做的字節字符串創建一個實際的PDF文件:
blob.upload_from_string(bytedata, content_type='application/pdf')
我的字節數據是b64encoded,所以我首先使用b64解碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.