为Google Cloud Storage API提供凭据 [英] Providing Credentials to Google Cloud Storage API

查看:703
本文介绍了为Google Cloud Storage API提供凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试组建一个用于测试Google云端存储的hello world程序。我的目标是拥有一个最简单的程序,只需将硬编码文件上传到云存储。

I'm trying to put together a hello world program for testing out Google Cloud Storage. My goal is to have the simplest possible program that just uploads a hard-coded file to Cloud Storage.

我一直在网上搜索基本教程,但是我能找到的最近的本指南,用于使用云端存储来自App Engine。我把这个程序放在一起:

I've been scouring the internet for a basic tutorial, but the closest I could find is this guide for using Cloud Storage from App Engine. I've put together this program:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;

import com.google.cloud.storage.Acl;
import com.google.cloud.storage.Acl.Role;
import com.google.cloud.storage.Acl.User;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

public class Main {
    public static void main(String[] args) throws FileNotFoundException{

        FileInputStream fileInputStream = new FileInputStream("my-file.txt");

        Storage storage = StorageOptions.getDefaultInstance().getService();

        BlobInfo blobInfo =
            storage.create(
                BlobInfo
                    .newBuilder("my-cloud-storage-bucket", "my-file.txt")
                    .setAcl(new ArrayList<>(Arrays.asList(Acl.of(User.ofAllUsers(), Role.READER))))
                    .build(),
                fileInputStream);

        String fileUrl = blobInfo.getMediaLink();

        System.out.println("Uploaded URL: " + fileUrl);
    }
}

当我运行该代码时,我得到一个<$当我打电话给 storage.create()时,c $ c> 401 Unauthorized 错误,这并不奇怪,因为我没有提供用户名或密码。我收集的是因为该示例在App Engine中运行,它以这种方式提供凭证。

When I run that code, I get a 401 Unauthorized error when I call storage.create(), and that's not surprising because I'm not providing a username or password. I gather that's because the example is running in App Engine, which provides the credentials that way.

我尝试通过属性文件传递我的凭据:

I've tried passing in my credentials via a properties file:

Credential credential = new GoogleCredential.Builder()
                .setTransport(httpTransport)
                .setJsonFactory(jsonFactory)
                .setServiceAccountId("accountId")
                .setServiceAccountPrivateKeyFromP12File(
                        new File("PRIVATE_KEY_PATH"))
                .setServiceAccountScopes(scopes).build();

Storage storage = new StorageOptions.DefaultStorageFactory().create(StorageOptions.newBuilder().setCredentials(credential).build());

但这不能编译,因为 setCredentials()函数需要凭据实例,而不是凭据实例。

But that doesn't compile because the setCredentials() function needs a Credentials instance, not a Credential instance.

我一直在谷歌搜索并阅读云存储API ,但我仍然无法弄清楚如何在不经过复杂的设置过程的情况下传递凭据。

I've been Googling and reading through the Cloud Storage API, but I still can't figure out how I'm supposed to pass in the credentials without going through a convoluted setup process.

所以,我的问题是:为Google云端存储提供凭据以上传文件的最简单方法是什么?

So, my question is: what is the simplest way to provide credentials to Google Cloud storage to upload a file?

对于某些背景:我我试图比较和对比谷歌云存储和亚马逊S3。我可以在S3中创建这样的程序没问题,但出于某种原因,我正在使用云存储打砖块。绝对欣赏任何人可以抛出的任何见解或基本教程。

For some background: I'm trying to compare and contrast Google Cloud Storage and Amazon S3. I can create a program like this in S3 no problem, but for some reason I'm hitting a brick wall with Cloud Storage. Definitely appreciate any insights or basic tutorials anybody can throw my way.

推荐答案

从PKCS创建凭据似乎并不那么容易#12文件包含新的Google Cloud Client Library,因为它曾与旧的
云存储JSON API

It seems it's not so easy to create credentials from a PKCS #12 file with new Google Cloud Client Library as it used to be with the old Cloud Storage JSON API.

最简单的方法是使用JSON格式,如此处,然后使用 GoogleCredentials#fromStream 加载它的方法:

The easiest way would be to use JSON format instead as described here, and then use GoogleCredentials#fromStream method to load it:

Credentials credentials = GoogleCredentials.fromStream(new FileInputStream("credentials.json"));
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

如果您仍想使用PKCS#12,我相信这样可行:

If you still want to use PKCS #12, I believe this would work:

PrivateKey privateKey = SecurityUtils.loadPrivateKeyFromKeyStore(
    SecurityUtils.getPkcs12KeyStore(), new FileInputStream("credentials.p12"), "notasecret", "privatekey", "notasecret");
Credentials credentials = new ServiceAccountCredentials(null, "accountId", privateKey, null, null);
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

这篇关于为Google Cloud Storage API提供凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆