【问题标题】:How to start google cloud storage resumable upload?如何启动谷歌云存储恢复上传?
【发布时间】:2014-02-26 00:06:33
【问题描述】:

我正在尝试在后端创建一个 GCS 可恢复上传 url,将其发送到前端以获取 javascript 以开始将大文件直接上传到存储桶。

我目前的问题是我不知道如何开始上传。

所有documentation 都没有任何代码,所以我没有任何依据。

目前我的代码是这样的:

def start_resumable_upload(self):
    API_ENDPOINT = (
        'https://www.googleapis.com/upload/storage/v1beta2/'
        'b%(bucket)s/o?uploadType=resumable&name=%(object_name)s'
    )
    url_params = {
        'bucket': BUCKET,
        'object_name': self.filename
    }
    headers = {
        'X-Upload-Content-Type': self.content_type,
        'X-Upload-Content-Length': 0,
        'Content-Type': 'application/json'
    }
    r = urlfetch.fetch(
        url=API_ENDPOINT % url_params,
        method=urlfetch.POST,
        headers=headers
    )
    return r.content

start_resumable_upload 是我创建的文件模型中的一个方法,用于跟踪数据库上的元数据,因此,self.filename 将具有文件名,content_type 将是 mime 类型等等。

该请求的响应是:

400. 这是一个错误。

您的客户发出了格式错误或非法的请求。我们知道的就这些。

这有点不可接受。

感谢任何帮助。

【问题讨论】:

  • 您不使用google-api-python-client的任何原因?
  • @jterrace 是的,我在文档中没有提到这一点。让我看看。 -- 我将如何使用开始可恢复上传?
  • BUCKET 是否有前导“/”?如果不是,您的 URL 格式不正确。您还需要对请求进行身份验证:developers.google.com/storage/docs/json_api/v1/how-tos/…
  • 是的,它确实有一个前导,它是 /projectname-bucket

标签: python google-app-engine google-cloud-storage


【解决方案1】:

sample application 包含使用google-api-python-client 在Python 中进行可恢复上传的示例。

关键设置是:

media = MediaFileUpload(filename, chunksize=CHUNKSIZE, resumable=True)
if not media.mimetype():
    media = MediaFileUpload(filename, DEFAULT_MIMETYPE, resumable=True)
request = service.objects().insert(bucket=bucket_name, name=object_name,
                                 media_body=media)

【讨论】:

  • 我采取了不同的方法。我使用签名的 url,更新了 GCS CORS,并使用 PUT 方法使用 xhr + Blob 上传了文件。这真是一段旅程......
【解决方案2】:

This function 将在使用基于文件的 JSON 服务帐户密钥进行身份验证后生成签名上传 URL。然后可以将其返回给客户端以通过 HTTP PUT 验证上传。

import json
import os
from oauth2client.service_account import ServiceAccountCredentials
import httplib2

GCP_CREDENTIALS_FILE = os.getenv('GCP_CREDENTIALS_FILE', 'client-secret.json')
GCS_UPLOAD_URL_PATTERN = 'https://www.googleapis.com/upload/storage'+ \
                         '/v1/b/{bucket}/o?uploadType=resumable'

def get_upload_url(bucket, filename, content_length, content_type='application/octet-stream',):
    credentials = ServiceAccountCredentials.from_json_keyfile_name(
        'client-secret.json',
        ('https://www.googleapis.com/auth/devstorage.read_write',),
    )
    http = httplib2.Http()
    credentials.authorize(http)
    url = GCS_UPLOAD_URL_PATTERN.format(bucket=bucket)
    body = json.dumps({
        'name': filename,
    }).encode('UTF-8')
    headers = {
        'X-Upload-Content-Type': content_type,
        'X-Upload-Content-Length': content_length,
        'Content-Type': 'application/json; charset=UTF-8',
        'Content-Length': len(body),
    }
    resp_headers, resp_body = http.request(url, method='POST', headers=headers, body=body)
    return resp_headers['location']

您还可以使用 App Engine 或 Compute Engine 凭据进行授权。然后您只需要更改用于生成credentials 实例的oauth2client 类。如果您要在生产中使用它,您还需要为凭据问题、网络问题等添加一些错误处理。

【讨论】:

    猜你喜欢
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 2013-10-02
    • 2012-12-09
    • 1970-01-01
    相关资源
    最近更新 更多