Insert an object (as a folder) into Google Cloud Storage using the JSON API with Java

I already know that there are no folders in Google Cloud Storage (they are treated as objects), so please take a look at my question and you can understand what I mean. Thank: -)

This is my method for loading an object, and in my particular case I would like to upload a "folder", so a file that ends with a "/" (for example: "newfolder /"), but I get a GoogleJson error in execute () at the end of my code.

[Error]:

Uncaught exception from servlet
com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 OK
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "message" : "Required",
    "reason" : "required"
  }, {
    "domain" : "global",
    "message" : "Required",
    "reason" : "required"
  }, {
    "domain" : "global",
    "message" : "Required",
    "reason" : "required"
  } ],
  "message" : "Required"
}

[my code inside the method]:

StorageObject objectMetadata = null;

        //default bucket
        String bucketRootName = "defaultbucketname";

        //First Folder
        String folderAppName = "myfolder";

        //folder that i want to create (upload)
        folderName+="/";
        String folderPath = folderAppName + "/" + folderName;

        if (useCustomMetadata) {

            List<ObjectAccessControl> acl = Lists.newArrayList();
            acl.add( new ObjectAccessControl().setEntity("allAuthenticatedUsers").setRole("OWNER"));

            objectMetadata = new StorageObject()

            .setName(folderPath)
            .setAcl(acl)
            .setContentDisposition("attachment");
        }

        Storage.Objects.Insert insertObject = storage.objects().insert(bucketRootName, objectMetadata);

        StorageObject metadata = insertObject.execute();
        return metadata;

To have a bucket with this path: defaultbucketname / my_folder / FOLDERNAME /

Does anyone know how to solve it?

Thank you very much

+4
2

Content-Type, .

, , , .

+2

, . , .

. foldA/foldB/test.txt, .

            InputStream is = Files.newInputStream(path); //path if java.nio.file.Path
            String mimeType = Files.probeContentType(path);
            InputStreamContent isContent = new InputStreamContent(mimeType, is);
            StorageObject objectMetadata = new StorageObject();

Storage.Objects.Insert insertObject = storage.objects(). insert (bucketRootName, objectMetadata, isContent);

            insertObject.setName("foldA/foldB/"+path.getFileName().toString()); // path.getFileName().toString() will give u test.txt
            insertObject.execute();
0

Source: https://habr.com/ru/post/1543020/


All Articles