我有一个脚本,可以从Web下载某些图像(<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)
答案 0 :(得分:1)
您可以使用GCS Python Client Library以编程方式执行与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客户端库并设置身份验证凭据。您可以找到here有关如何执行这些步骤的信息。
答案 1 :(得分:0)
尝试一下:https://cloud.google.com/python
记住,请始终检查是否有您想要的库。