Skip to content

Commit

Permalink
[#187456042] fixed NPE when calling to get GCP blob metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
patmagee committed Apr 25, 2024
1 parent 981ea77 commit dfc37e8
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/main/java/com/dnastack/wes/storage/GcpBlobStorageClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ public GcpBlobStorageClient(GcpBlobStorageConfig config) throws IOException {

@Override
public URL getSignedUrl(String blobUri) {
BlobId blobId = GcpStorageUtils.blobIdFromGsUrl(blobUri);
Blob blob = client.get(blobId);
Blob blob = getBlob(blobUri);
return blob.signUrl(ttl, TimeUnit.MILLISECONDS, SignUrlOption.httpMethod(HttpMethod.GET), SignUrlOption
.httpMethod(HttpMethod.HEAD));
}
Expand All @@ -66,14 +65,13 @@ public URL getSignedUrl(String blobUri) {
public String writeBytes(InputStream blobStream, long size, String stagingFolder, String blobName) throws IOException {
String blobUri = UriComponentsBuilder.fromUri(stagingLocation).pathSegment(stagingFolder, blobName)
.toUriString();
BlobId blobId = GcpStorageUtils.blobIdFromGsUrl(blobUri);
Blob blob = client.get(blobId);
if (blob != null && blob.exists()) {
throw new IOException("An in the current staging directory with the name: " + blobName
if (doesFileExist(blobUri)) {
throw new IOException("A blob in the current staging directory with the name: " + blobName
+ " already exists. Could not overrwrite file");
}

blob = client.create(BlobInfo.newBuilder(blobId).build());
BlobId blobId = getBlobId(blobUri);
Blob blob = client.create(BlobInfo.newBuilder(blobId).build());
try (WriteChannel writeChannel = blob.writer(BlobWriteOption.userProject(project))) {
ByteBuffer byteBuffer = ByteBuffer.allocate(64 * 1024);
while (blobStream.available() > 0) {
Expand All @@ -89,8 +87,7 @@ public String writeBytes(InputStream blobStream, long size, String stagingFolder

@Override
public void readBytes(OutputStream outputStream, String blobUri, HttpRange httpRange) throws IOException {
BlobId blobId = GcpStorageUtils.blobIdFromGsUrl(blobUri);
Blob blob = client.get(blobId);
Blob blob = getBlob(blobUri);

if (blob == null || !blob.exists()) {
throw new FileNotFoundException("Could not open open file: " + blobUri + " it does not appear to exist");
Expand Down Expand Up @@ -128,26 +125,33 @@ public BlobId getBlobId(String filePath) {
return GcpStorageUtils.blobIdFromGsUrl(filePath);
}

Blob getBlob(String filePath) {
return client.get(getBlobId(filePath));
}


@Override
public boolean doesFileExist(String filePath) {
try {
return client.get(GcpStorageUtils.blobIdFromGsUrl(filePath)).exists();
} catch (Exception e){
return getBlob(filePath).exists();
} catch (Exception e) {
return false;

}
}

@Override
public void deleteFile(String filePath) {
client.delete(GcpStorageUtils.blobIdFromGsUrl(filePath));
if (!doesFileExist(filePath)) {
throw new NotFoundException("File: " + filePath + ", does does not exist");
}

client.delete(getBlobId(filePath));
}

@Override
public BlobMetadata getBlobMetadata(String filePath) {
Blob blob = client.get(GcpStorageUtils.blobIdFromGsUrl(filePath));
if (Boolean.FALSE.equals(blob.exists())) {
Blob blob = getBlob(filePath);
if (blob == null || Boolean.FALSE.equals(blob.exists())) {
throw new NotFoundException("File: " + filePath + ", does does not exist");
}

Expand Down

0 comments on commit dfc37e8

Please sign in to comment.