1

I am using the "google-api-services-storage-v1beta2-rev5-java-1.15.0-rc.zip" Google Cloud Storage library together with the "StorageSample.java" sample program from here

I have followed the sample program's setup instructions and have set up the "client_secrets.json" and "sample_settings.json" files. The sample program compiles OK but runs only partially OK.

I have modified the "uploadObject" method of the "StorageSample.java" program so that it uploads a test file created by me (rather than upload a randomly generated file). The program runs OK in the following methods :

tryCreateBucket();
getBucket();
listObjects();
getObjectMetadata();

However, when running the "uploadObject(true)" method, I get the following error

================== Uploading object. ==================

Forbidden

My modified "uploadObject" method is listed below :

private static void uploadObject(boolean useCustomMetadata) throws IOException {
  View.header1("Uploading object.");
  File file = new File("My_test_upload_file.txt");
  if (!file.exists() || !file.isFile()) {
      System.out.println("File does not exist");
      System.exit(1);
  }
  InputStream inputStream = new FileInputStream(file);
  long byteCount = file.length();
  InputStreamContent mediaContent = new InputStreamContent("application/octet-stream", inputStream);
  mediaContent.setLength(byteCount);
  StorageObject objectMetadata = null;
  if (useCustomMetadata) {
      List<ObjectAccessControl> acl = Lists.newArrayList();  // empty acl (seems default acl).
      objectMetadata = new StorageObject()
             .setName("myobject")
             .setMetadata(ImmutableMap.of("key1", "value1", "key2", "value2"))
             .setAcl(acl)
             .setContentDisposition("attachment");        
  }
  Storage.Objects.Insert insertObject = storage.objects().insert("mybucket", objectMetadata, mediaContent);
  if (!useCustomMetadata) {
    insertObject.setName("myobject");
  }
  if (mediaContent.getLength() > 0 && mediaContent.getLength() <= 2 * 1000 * 1000 /* 2MB */) {
    insertObject.getMediaHttpUploader().setDirectUploadEnabled(true);
  }
  insertObject.execute();
}

In the 1st run of the program, a bucket is created and I get the "Forbidden" error when uploading my created test file. In subsequent runs, the "Forbidden" errors persist.

I think that as the bucket is created by the program, the program should have enough access right to upload a file to that bucket.

Is there any setup / operation that I have missed ? Thanks for any suggestion.

4

1 回答 1

3

哦,多么粗心的错误。我忘记将“mybucket”名称更改为我创建的存储桶的名称。

该程序现在运行正常。

于 2013-06-19T15:14:48.413 回答