如何通过Python脚本在Google Cloud Storage上上传字节图像

Dav*_*ghi 5 python opencv google-cloud-storage google-cloud-platform

我想通过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()
Run Code Online (Sandbox Code Playgroud)

如果gcs_image = open('img.jpg', 'r')代码有效并且可以将我的映像正确保存在Cloud Storage中。如何直接上传字节图像?(例如,从一个的OpenCV / numpy的数组:gcs_image = cv2.imread('img.jpg')

Ram*_*raj 8

如果您想从文件上传图像。

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
Run Code Online (Sandbox Code Playgroud)

但如果你想直接上传字节:

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
Run Code Online (Sandbox Code Playgroud)

请注意,这target_key是前缀和上传文件的名称。


Mel*_*Guo 8

就我而言,我想将 PDF 文档从字节上传到 Cloud Storage。

当我尝试以下操作时,它创建了一个文本文件,其中包含我的字节字符串。

blob.upload_from_string(bytedata)
Run Code Online (Sandbox Code Playgroud)

为了使用字节字符串创建实际的 PDF 文件,我必须执行以下操作:

blob.upload_from_string(bytedata, content_type='application/pdf')
Run Code Online (Sandbox Code Playgroud)

我的字节数据是 b64encoded,所以我也有 b64decode 首先它。


A.Q*_*eue 1

MediaIoBaseUpload需要一个io.Base类似的对象并引发以下错误:

\n\n
  \'numpy.ndarray\' object has no attribute \'seek\'\n
Run Code Online (Sandbox Code Playgroud)\n\n

收到 ndarray 对象后。为了解决这个问题,我正在使用TemporaryFilenumpy.ndarray().tofile()

\n\n
from oauth2client.service_account import ServiceAccountCredentials\nfrom googleapiclient import discovery\nimport googleapiclient\nimport numpy as np\nimport cv2\nfrom tempfile import TemporaryFile\n\n\nscopes = [\'https://www.googleapis.com/auth/devstorage.full_control\']\ncredentials = ServiceAccountCredentials.from_json_keyfile_name(\'serviceAccount.json\', scopes)\nservice = discovery.build(\'storage\',\'v1\',credentials = credentials)\n\nbody = {\'name\':\'my_image.jpg\'}\nwith TemporaryFile() as gcs_image:\n    cv2.imread(\'img.jpg\').tofile(gcs_image)\n    req = service.objects().insert(\n       bucket=\'my_bucket\xe2\x80\x99, body=body,\n       media_body=googleapiclient.http.MediaIoBaseUpload(\n          gcs_image, \'application/octet-stream\'))\n\n    resp = req.execute()\n
Run Code Online (Sandbox Code Playgroud)\n\n

请注意,googleapiclient 不是惯用的,仅用于维护(它\xe2\x80\x99s 不再开发)。我建议使用惯用的

\n