如何访问谷歌云存储中的对象?

我试图build立一个应用程序,将访问我的谷歌云存储中的文件,而桶是私人的。 文档不清楚如何做,以便从服务器访问存储桶。 我有他们提供的JSON文件。 我打算使用nodejs公开桶上的文件,但我不知道如何授权请求到桶。 我正在使用axios。 我是否创build一个API键或使用一个键作为查询string? 一些帮助将不胜感激!

您需要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

尝试这个:

  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() 

你的文件将是你提到的“本地文件名”。 上面的代码是在Python中。