将 .raw 文件上传到 Google 云存储

Upload .raw files to Google Cloud Storage

我有一个 Python 脚本试图在 google 存储上上传文件,但一直出现此错误:

googleapiclient.errors.UnknownFileType:

这是我的代码:

def create_service():

    credentials = GoogleCredentials.get_application_default()

    return build('storage', 'v1', credentials=credentials)

service=create_service()

request=service.objects().insert(bucket='testBucket',name='test.raw',media_body=path_files+file_raw)

response=request.execute()

exit()

file_raw 是音频 .raw 文件。

我使用的库是:

from oauth2client.client import GoogleCredentials

from googleapiclient.discovery import build

from google.cloud import storage

我相信,因为您的文件以“.raw”结尾,"mimetypes" 库无法猜测您想要的对象的内容类型。尝试使用您希望 GCS 使用的 MIME 类型明确指定 "media_mime_type" 关键字参数。

这是我认为可能产生错误的地方:https://github.com/google/google-api-python-client/blob/master/googleapiclient/discovery.py#L789

另外,我建议你看看Google Cloud Client Library for Python。我发现它更好用。