如何访问 google 云存储中的对象?
How to access objects in google cloud storage?
我正在尝试构建一个应用程序,该应用程序将在存储桶私有时访问我的 google 云存储中的文件。文档不清楚如何才能从服务器访问存储桶。我有他们提供的 json 文件。我计划使用 nodejs 公开存储桶上的文件,但我不确定如何授权对存储桶的请求。顺便说一下,我正在使用 axios。我是创建 API 键还是使用键作为查询字符串?一些帮助将不胜感激!
试试这个:
scopes = ['https://www.googleapis.com/auth/devstorage.read_only']
cred = ServiceAccountCredentials.from_json_keyfile_name('<path to .json credential file>', scopes)
bucket = '<your bucket name>'
google_service = build('storage', 'v1', credentials=cred)
req = google_service.objects().get_media(bucket=bucket,object='<your cloud object name>')
media = MediaIoBaseDownload(<local file name>, req, chunksize=1024*1024)
resp = None
while resp is None:
status, resp = media.next_chunk()
您的文件将是您提到的 "local file name"。以上代码在Python.
你想要 Google Cloud 的 Node.JS 图书馆:https://googlecloudplatform.github.io/google-cloud-node/#/
下面是使用它访问 GCS 中对象的示例:
var config = {
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
};
var storage = require('@google-cloud/storage')(config);
var bucket = gcs.bucket('my-existing-bucket');
var remoteReadStream = bucket.file('giraffe.jpg').createReadStream();
remoteReadStream.pipe(someLocalWriteStream);
GitHub 页面上还有其他几个示例:https://github.com/GoogleCloudPlatform/google-cloud-node#preview-1
以及此处的大量文档:
https://googlecloudplatform.github.io/google-cloud-node/#/docs/google-cloud/0.56.0/storage
我正在尝试构建一个应用程序,该应用程序将在存储桶私有时访问我的 google 云存储中的文件。文档不清楚如何才能从服务器访问存储桶。我有他们提供的 json 文件。我计划使用 nodejs 公开存储桶上的文件,但我不确定如何授权对存储桶的请求。顺便说一下,我正在使用 axios。我是创建 API 键还是使用键作为查询字符串?一些帮助将不胜感激!
试试这个:
scopes = ['https://www.googleapis.com/auth/devstorage.read_only']
cred = ServiceAccountCredentials.from_json_keyfile_name('<path to .json credential file>', scopes)
bucket = '<your bucket name>'
google_service = build('storage', 'v1', credentials=cred)
req = google_service.objects().get_media(bucket=bucket,object='<your cloud object name>')
media = MediaIoBaseDownload(<local file name>, req, chunksize=1024*1024)
resp = None
while resp is None:
status, resp = media.next_chunk()
您的文件将是您提到的 "local file name"。以上代码在Python.
你想要 Google Cloud 的 Node.JS 图书馆:https://googlecloudplatform.github.io/google-cloud-node/#/
下面是使用它访问 GCS 中对象的示例:
var config = {
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
};
var storage = require('@google-cloud/storage')(config);
var bucket = gcs.bucket('my-existing-bucket');
var remoteReadStream = bucket.file('giraffe.jpg').createReadStream();
remoteReadStream.pipe(someLocalWriteStream);
GitHub 页面上还有其他几个示例:https://github.com/GoogleCloudPlatform/google-cloud-node#preview-1
以及此处的大量文档: https://googlecloudplatform.github.io/google-cloud-node/#/docs/google-cloud/0.56.0/storage