- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
基本上,问题出在标题中:我不知道如何将文件上传到 Google 存储空间
我查看了这个网页:
https://cloud.google.com/storage/docs/uploading-objects
Java 语言选项卡中的“上传对象”建议使用这两行:
InputStream content = new ByteArrayInputStream("Hello, World!".getBytes(UTF_8));
Blob blob = bucket.create(blobName, content, "text/plain");
上面的bucket
对象可以从storage
对象中接收到,这才是问题的根源
在目前为止我看过的所有教程中(尤其是这个:https://support.google.com/dfp_premium/answer/1733127?hl=en),storage
属于 com.google.api.services.storage.Storage
而不是 com.google.cloud.storage.Storage
,实例化如下:
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JacksonFactory.getDefaultInstance())
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(
Collections.singleton(STORAGE_SCOPE))
.setServiceAccountPrivateKeyFromP12File(p12File).build();
storage = new com.google.api.services.storage.Storage.Builder(httpTransport,
JacksonFactory.getDefaultInstance(), credential)
.setApplicationName(PROJECT_NAME).build();
这样创建存储对象后,我们可能会得到一个bucket对象,如下:
com.google.api.services.storage.Storage.Buckets.Get object = storage.buckets().get(BUCKET_NAME);
com.google.api.services.storage.model.Bucket bucket = object.execute();
但是我不能使用 com.google.api.services.storage.model.Bucket
上传文件,因为它没有提到 create()
方法多于。我需要改用 com.google.cloud.storage.Bucket
。
我也检查了这个 stackoverflow 问题:Upload image to Google Cloud Storage (Java)
并且所提供链接中的示例确实使用了 com.google.cloud.storage.Storage
和 com.google.cloud.storage.Bucket
但它们从不显示如何传递凭据并实例化 storage
对象,类似于上面的代码。
有人可以提供一个清晰而全面的 Java 代码片段来展示如何将文件上传到 Google Cloud Storage,包括 storage
对象的所有必要凭证实例吗?
编辑。换句话说,我需要像上面那样自定义 com.google.cloud.storage.Storage
的实例化,而不是使用这一行:Storage storage = StorageOptions.getDefaultInstance().getService( );
编辑2。现在,我终于可以通过以下代码检索存储桶后上传文件了:
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(KEY_JSN)).createScoped(Lists.newArrayList("https://www.googleapis.com/auth/devstorage.read_write"));
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
Bucket bucket = storage.get(BUCKET_NAME);
对我来说还有一个微妙的地方,就是下面这行:
Page<Bucket> buckets = storage.list();
返回一个空列表。在能够通过 get(bucket_name)
接收特定存储桶元数据的同时接收空存储桶列表是否有意义?
最佳答案
我能够使用 com.google.cloud.storage
库创建并上传到存储桶,以下代码段来自 documentation :
public void createBlobFromByteArray(String blobName, String jsonPath, String bucketName) throws IOException {
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath)).createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
Bucket bucket = storage.create(BucketInfo.of(bucketName));
Blob blob = bucket.create(blobName, "Hello, World!".getBytes(UTF_8), "text/plain");
}
您需要create a JSON service account key in and download it from the Google Cloud console .然后在GoogleCredentials
方法中传递给'storage'
关于java - 将文件上传到 Google Cloud Storage (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51358617/
我是一名优秀的程序员,十分优秀!