How to create an empty folder in Google Cloud using Java API

I am using the JSON API, the Google API Client Library for Java, to access objects from the Google Cloud Storage. I need to create (do not load) an empty folder in a bucket. The Google Developer Web Console has this ability to create a directory , but in the Java API or the gsutil command, both versions do not have the create folder command. If anyone knows, please let me know. Thanks at Advance ...

+4
source share
3 answers

You can emulate a folder by loading a zero-sized object with a trailing slash.

, Google Cloud Storage , .

+3

Java () :

public static void createFolder(String name) throws IOException {
    Channels.newOutputStream(
            createFile(name + "/")
    ).close();
}

public static GcsOutputChannel createFile(String name) throws IOException {
    return getService().createOrReplace(
            new GcsFilename(getName(), name),
            GcsFileOptions.getDefaultInstance()
    );
}

private static String name;

public static String getName() {
    if (name == null) {
        name = AppIdentityServiceFactory.getAppIdentityService().getDefaultGcsBucketName();
    }
    return name;
}

public static GcsService service;

public static GcsService getService() {
    if (service == null) {
        service = GcsServiceFactory.createGcsService(
                new RetryParams.Builder()
                        .initialRetryDelayMillis(10)
                        .retryMaxAttempts(10)
                        .totalRetryPeriodMillis(15000)
                        .build());
    }
    return service;
}
+1

, . , images , docs, , : images/name_of_file docs/name_of_file strong > .

images/dogImage, , .

.

+1

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


All Articles