Comments (6)
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.
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.
What URL does that sample code come from? It has an issue.
from java-storage.
Which sample code do you mean?
from java-storage.
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.
from java-storage.
Related Issues (20)
- Improve Virtual Thread compatibility
- Unable to initiate resumable uploads from java sdk: Cloud storage v2 API. HOT 4
- Unable to Get or Refresh access token from Google Credentials instance in java sdk HOT 1
- Novel transient error introduced between 2.27.1 and 2.30.1? HOT 5
- Add Support for includeFolders option in ListObjects HOT 3
- Fix retry logic on SSL Connection Reset errors and Error writing request body to server HOT 1
- storage: memory leak while writing to GCS bucket HOT 6
- Uploading files to GCS via WriteChannel cause the memory to increase - memory not released HOT 3
- Use opentelemetry for tracing google-cloud-storage client calls
- Storage.writer(...) breaks the idea of 'generation' HOT 2
- Support for Storage Control Quickstart
- Issue with GCS matchGlob method HOT 3
- Jqwik Usage Survey HOT 2
- createFrom(BlobInfo blobInfo, Path path, int bufferSize, BlobWriteOption... options) ignores bufferSize HOT 2
- allow cancelling a resumable upload HOT 1
- allow specifying the content length for resumable uploads HOT 2
- storage: PCU hangs HOT 9
- Checksum verification when uploading file to cloud storage not working - version 2.36.1 HOT 2
- transfer manager PCUs should allowing specifying a key prefix for intermediate objects
- GCS : Spring Boot service gets blocked with virtual thread and library version >= 2.25.0 during file upload HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from java-storage.