Giter Club home page Giter Club logo

Comments (6)

dmitry-fa avatar dmitry-fa commented on July 28, 2024

For those who want to play around with it:
Setting versioning on a bucket with gsutil:
#> gsutil versioning set on gs://bucket_versioning_on
#> gsutil versioning set off gs://bucket_versioning_off

The code:

package storage;

import com.google.cloud.WriteChannel;
import com.google.cloud.storage.*;
import java.nio.ByteBuffer;

import static java.nio.charset.StandardCharsets.UTF_8;

public class Writer {

    public static void main(String... args) throws Exception {

        Storage storage = StorageOptions.getDefaultInstance().getService();
        String blobName = "my-blob-name";
        String bucketName = "bucket_versioning_off";

        BlobId blobId = BlobId.of(bucketName, blobName);
        byte[] content = "Hello, World!".getBytes(UTF_8);
        BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
        Blob blob = storage.create(blobInfo);
        System.out.println("-- initial --");
        System.out.println(blob);
        System.out.println("size: " + blob.getContent().length);

        try (WriteChannel writer = blob.writer()) {
            writer.write(ByteBuffer.wrap(content));
        }
        System.out.println("\n-- right after upload --");
        System.out.println(blob);
        try {
            System.out.println("content '" + new String(blob.getContent()) + "'");
        } catch (StorageException e) {
            System.out.println("content " + e.getMessage());
        }

        System.out.println("\n-- after blob = blob.reload(Blob.BlobSourceOption.generationNotMatch()) --");

        try {
            blob = blob.reload(Blob.BlobSourceOption.generationNotMatch());
            System.out.println(blob);
        } catch (StorageException e) {
            System.out.println("exception: " + e.getMessage());
            System.out.println("\n-- after blob = blob.reload() --");
            blob = blob.reload();
            System.out.println(blob);
        }

        if (blob != null) {
            System.out.println("\n-- after blob = storage.get(blob.getBlobId()) --");
            blob = storage.get(blob.getBlobId());
            System.out.println(blob);
        }

        System.out.println("\n-- after blob = storage.get(blobId) --");
        blob = storage.get(blobId);
        System.out.println(blob);
    }
}

from java-storage.

dmitry-fa avatar dmitry-fa commented on July 28, 2024

Blob.reload() is just a shortcut method for Storage.get(BlobId). BlobId includes bucket,name and generation. If generation is not specified Storage.get(blobId) returns the latest version. If generation is given and differs from the latest version the output depends on the versioning setting of the bucket: If versioning is enabled Storage.get(BlobId) returns not the latest version, if disabled then null is returned, which means: 'object not found'. Blob.reload() always use generation, so if the content is changed reload() will never return the latest version. Such behavior is not intuitively expected.

Possible fix is to make Blob.reload() to not include generation in the get request.

from java-storage.

elharo avatar elharo commented on July 28, 2024

What URL does that sample code come from? It has an issue.

from java-storage.

dmitry-fa avatar dmitry-fa commented on July 28, 2024

Which sample code do you mean?

from java-storage.

elharo avatar elharo commented on July 28, 2024

byte[] content = "Hello, World!".getBytes(UTF_8);
try (WriteChannel writer = blob.writer()) {
try {
writer.write(ByteBuffer.wrap(content, 0, content.length));
} catch (Exception ex) {
// handle exception
}
}

from java-storage.

dmitry-fa avatar dmitry-fa commented on July 28, 2024

https://googleapis.dev/java/google-cloud-storage/latest/com/google/cloud/storage/Blob.html#writer-com.google.cloud.storage.Storage.BlobWriteOption...-

from java-storage.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.