在我的本地计算机上运行以下代码 以在 google存储上创建新存储桶,我收到错误消息“ StorageException:存储桶的帐户”my -new-bucket-abc“已被禁用”。
String SERVICE_ACCOUNT_JSON_PATH = "C:\\gcpconfig\\My First Project-6f9cff47c4f0.json";
Storage storage =
StorageOptions.newBuilder()
.setCredentials(
ServiceAccountCredentials.fromStream(
new FileInputStream(SERVICE_ACCOUNT_JSON_PATH)))
.build()
.getService();
String bucketName = "my-new-bucket-abc";
// Creates the new bucket
Bucket bucket = storage.create(BucketInfo.of(bucketName));
System.out.printf("Bucket %s created.%n", bucket.getName());
但是,我可以通过运行以下代码从谷歌存储中下载文件:
String SERVICE_ACCOUNT_JSON_PATH = "C:\\gcpconfig\\My First Project-6f9cff47c4f0.json";
Storage storage =
StorageOptions.newBuilder()
.setCredentials(
ServiceAccountCredentials.fromStream(
new FileInputStream(SERVICE_ACCOUNT_JSON_PATH)))
.build()
.getService();
BlobId blobId = BlobId.of("nmjcloud_jar_test","addons/simple-bean-1.0.jar");
Blob blob = storage.get(blobId);
Path path = Paths.get("D:\\lib\\simple-bean-1.0.jar");
blob.downloadTo(path);
System.out.printf("Download successfully%n");
完整的错误消息如下:
Exception in thread "main" com.google.cloud.storage.StorageException: The account for bucket "my-new-bucket-abc" has been disabled.
at com.google.cloud.storage.spi.v1.HttpStorageRpc.translate(HttpStorageRpc.java:191)
at com.google.cloud.storage.spi.v1.HttpStorageRpc.create(HttpStorageRpc.java:221)
at com.google.cloud.storage.StorageImpl$2.call(StorageImpl.java:112)
at com.google.cloud.storage.StorageImpl$2.call(StorageImpl.java:109)
at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:89)
at com.google.cloud.RetryHelper.run(RetryHelper.java:74)
at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:51)
at com.google.cloud.storage.StorageImpl.create(StorageImpl.java:108)
at com.example.storage.QuickstartSample.main(QuickstartSample.java:58)
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"location" : "Authorization",
"locationType" : "header",
"message" : "The account for bucket \"my-new-bucket-abc\" has been disabled.",
"reason" : "accountDisabled"
} ],
"message" : "The account for bucket \"my-new-bucket-abc\" has been disabled."
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at com.google.cloud.storage.spi.v1.HttpStorageRpc.create(HttpStorageRpc.java:219)
... 7 more
这是否意味着我使用的帐户无权创建存储区?
在使用服务帐户密钥的GCP VM上运行时,出现以下错误:
Caused by: com.google.cloud.storage.StorageException: Insufficient Permission
at com.google.cloud.storage.spi.v1.HttpStorageRpc.translate (HttpStorageRpc.java:191)
at com.google.cloud.storage.spi.v1.HttpStorageRpc.create (HttpStorageRpc.java:221)
at com.google.cloud.storage.StorageImpl$2.call (StorageImpl.java:112)
at com.google.cloud.storage.StorageImpl$2.call (StorageImpl.java:109)
at com.google.api.gax.retrying.DirectRetryingExecutor.submit (DirectRetryingExecutor.java:89)
at com.google.cloud.RetryHelper.run (RetryHelper.java:74)
at com.google.cloud.RetryHelper.runWithRetries (RetryHelper.java:51)
at com.google.cloud.storage.StorageImpl.create (StorageImpl.java:108)
at com.example.storage.QuickstartSample.createBucket (QuickstartSample.java:33)
at com.example.storage.QuickstartSample.main (QuickstartSample.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:282)
at java.lang.Thread.run (Thread.java:748)
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Insufficient Permission",
"reason" : "insufficientPermissions"
} ],
"message" : "Insufficient Permission"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from (GoogleJsonResponseException.java:146)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError (AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError (AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse (AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute (HttpRequest.java:1065)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed (AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed (AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute (AbstractGoogleClientRequest.java:469)
at com.google.cloud.storage.spi.v1.HttpStorageRpc.create (HttpStorageRpc.java:219)
at com.google.cloud.storage.StorageImpl$2.call (StorageImpl.java:112)
at com.google.cloud.storage.StorageImpl$2.call (StorageImpl.java:109)
at com.google.api.gax.retrying.DirectRetryingExecutor.submit (DirectRetryingExecutor.java:89)
at com.google.cloud.RetryHelper.run (RetryHelper.java:74)
at com.google.cloud.RetryHelper.runWithRetries (RetryHelper.java:51)
at com.google.cloud.storage.StorageImpl.create (StorageImpl.java:108)
at com.example.storage.QuickstartSample.createBucket (QuickstartSample.java:33)
at com.example.storage.QuickstartSample.main (QuickstartSample.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:282)
at java.lang.Thread.run (Thread.java:748)
我在GCP VM上再次尝试过,现在可以正常工作了。
关于我的Google云端平台的一些设置:
2。服务帐户密钥:
{
"type": "service_account",
"project_id": "ornate-shine-192301",
"private_key_id": "6f9cff47c4f0f8b1263bbf515f7e3318dcc88487",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDHvLTQn5i0Aheb\nW9iUkaRpZiGACUrh694JAzckVPEdhDoLaV4ZBapnmYrJBqQIs8RFXRWajtOrjzbY\nKJXWhOdtIim72fB0KVENjTRmq2m+wWf+ks19/eWakzYlXvsry61GT44UfccCOYbU\nioyevhQtJ0mdpa7QtAtjWyFmNXHodLjHHiwhArRYRP4Mel0MkHKi64EZ5aDLEGer\nkjntqnDcni0giCQNcvJw8c9XBB64dDcldSc7ZZ8QGXgBwzqYCt1F7pjK8rg6CFBi\n8ZKmUaRpguG4pB229fuwTWwE++/WEUtmgvGV4iQ6u6Zzeiw8TPKzgYzrF7XmBu38\nzHa+5wkJAgMBAAECggEAOwz7yhf9De72arx/LgFXAcArlE5rAJr6pGB6e3fPZyIS\nBRCsPKqTpBhdUyxu75GyK62m9vBV/QkUtlhdYDQftYCdpQd8UuNqLp76CIaR9073\nv2hIb4DSUl5GaksH5/1J55kakM1UukAmsAycIUxh662LUp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Rx2M6HeLE277syJxBNgU\n3DsGix3gso9/jJjt4p5G5II=\n-----END PRIVATE KEY-----\n",
"client_email": "gcsserviceaccountname1@ornate-shine-192301.iam.gserviceaccount.com",
"client_id": "114270155500502944764",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gcsserviceaccountname1%40ornate-shine-192301.iam.gserviceaccount.com"
}
补充:
1.我可以使用gsutil
命令行或GCP console
创建存储桶
2. Java API 可以从GCS下载文件
3.在 GCP VM 上,我可以使用带有应用程序默认凭据的Java API创建存储桶。(update20180316)
您正在使用Google Compute Engine虚拟机。与此虚拟机关联的服务凭据将由Application Default Credentials自动使用,因此无需使用此命令(
gcloud auth application-default login
)。
参考链接:
1. APIs & Reference
2. Cloud Storage Client Libraries
3. Creating Storage Buckets
答案 0 :(得分:1)
可能是本地环境错误,如环境变量名称,字符串引号等。