刚刚开始使用 GCS 及其 Java API。改编了 Google Plus示例并尝试检索存储桶。
我得到错误:
400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Invalid Value",
"reason" : "invalid"
} ],
"message" : "Invalid Value"
}
这是我的相关代码:
主要的:
public static void main(String[] args)
{
try
{
try
{
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
// service account credential (uncomment setServiceAccountUser for domain-wide delegation)
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(StorageScopes.DEVSTORAGE_READ_WRITE)
.setServiceAccountPrivateKeyFromP12File(new File(KEY_FILE_NAME))
// .setServiceAccountUser("user@example.com")
.build();
// set up global Storage instance
storage = new Storage.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
// run commands
getBucket();
// success!
return;
} catch (IOException e)
{
System.err.println(e.getMessage());
}
} catch (Throwable t)
{
t.printStackTrace();
}
System.exit(1);
}
获取桶方法:
/** Get a BUCKET??? for which we already know the ID. */
private static void getBucket() throws IOException
{
View.header1("Retrieve a bucket by name.");
String bucketName = "gs://gsdogs";
Bucket bucket = storage.buckets().get(bucketName).execute();
View.show(bucket);
}
常数:
private static final String CLIENT_ID = "************.apps.googleusercontent.com";
private static final String KEY_FILE_NAME = "privatekey.p12";
private static final String APPLICATION_NAME = "Elf-MobileCCtv/1.0";
/** E-mail address of the service account. */
private static final String SERVICE_ACCOUNT_EMAIL = "************@developer.gserviceaccount.com";
/** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT;
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
// We are using GCS not Google plus.
private static Storage storage;
提前致谢!