![](/img/trans.png)
[英]How to upload to google cloud storage an image using Python, without saving the image on my local disk?
[英]Upload images to Google Cloud instead of saving to disk (Python)
我有一個腳本,它從網絡下載某些圖像(<1 MB)並保存到本地磁盤。 我可以將它保存到 Google Cloud Storage 存儲分區而不是本地系統嗎?
def downloadImage(url):
try:
print("Downloading {}".format(url))
image_name = str(url).split('/')[-1]
resp = urlopen(url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
cv2.imwrite(current_path + "\\Downloaded\\" + image_name, image)
except Exception as error:
print(error)
您可以使用GCS Python 客戶端庫以編程方式執行與 GCS 相關的任務。 以下代碼會將位於/PATH/TO/SOURCE_FILE
的本地文件上傳到 GCS 存儲桶gs://BUCKET_NAME
from google.cloud import storage
def upload_blob(bucket_name, source_file_name, destination_blob_name):
"""Uploads a file to the bucket"""
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
print(
"File {} uploaded to {}.".format(
source_file_name, destination_blob_name
)
)
BUCKET_NAME = "BUCKET_NAME"
SOURCE_FILE_NAME = "/PATH/TO/SOURCE_FILE"
DESTINATION_FILE_NAME = "DESTINATION_FILE"
upload_blob(BUCKET_NAME, SOURCE_FILE_NAME, DESTINATION_FILE_NAME)
請記住,為了使用upload_blob
方法,必須安裝 GCS Python 客戶端庫並設置身份驗證憑據。 您可以在此處找到有關如何實施這些步驟的信息。
試試這個: https : //cloud.google.com/python
請記住,始終檢查是否有您想要的庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.