0

I have successfully uploaded the object to the google cloud storage using the Java library code as below

public static void uploadObject(
      String projectId, String bucketName, String objectName, String filePath) throws IOException {
    
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    BlobId blobId = BlobId.of(bucketName, objectName);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
    storage.create(blobInfo, Files.readAllBytes(Paths.get(filePath)));

    System.out.println(
        "File " + filePath + " uploaded to bucket " + bucketName + " as " + objectName);
  }

enter image description here

If I browse the public URL

If I browse the URL https://storage.googleapis.com/fetebird-product/bird.jpg the file is the downloadable link. How can I make it just a simple browsing file

What I am missing?

San Jaisy
  • 15,327
  • 34
  • 171
  • 290
  • I dont understand what you mean by simple browsing file? When clicking on bird.jpg I was able to download it. What do you want to do? – JCompetence Mar 11 '21 at 07:37
  • I should not download, it should open in the browser, because I need map all this URL to img src and when people browse the image it should view in browser instead of downloading – San Jaisy Mar 11 '21 at 07:38

1 Answers1

1

This is an answer from Googling/StackOverflow, which states:

How do I force files to open in the browser instead of downloading (PDF)?

To indicate to the browser that the file should be viewed in the browser, the HTTP response should include these headers:

Content-Type: application/pdf Content-Disposition: inline; filename="filename.pdf"

To have the file downloaded rather than viewed:

Content-Type: application/pdf Content-Disposition: attachment; filename="filename.pdf"

The quotes around the filename are required if the filename contains special characters such as filename[1].pdf which may otherwise break the browser's ability to handle the response.

How you set the HTTP response headers will depend on your HTTP server (or, if you are generating the PDF response from server-side code: your server-side programming language).

Therefore, in your logic you need to do:

BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("image/jpeg").setContentDisposition(String.format("inline; filename=\"%s\"", yourPictureFileName)).build();
JCompetence
  • 6,997
  • 3
  • 19
  • 26
  • Thank you, it worked for me, can you ask you one more favor. How do I get just uploaded image public URL in java library. From the documentation I can see https://storage.googleapis.com/BUCKET_NAME/OBJECT_NAME for public URL. But is there any way I can get this URL in java code after uploading the image – San Jaisy Mar 11 '21 at 09:22
  • @SanJaisy hmm not sure. Cant you just construct it yourself? https://storage.googleapis.com/{bucket.name}/{blob.name} If you already know the bucket name? and you know the file name....then its a String concaternation – JCompetence Mar 11 '21 at 10:46
  • @SanJaisy try Blob b = storage.create(xxxxxxx); b.getMetaData() <-- This returns a hashmap of key/value...see if the URL is 1 of them :) good luck – JCompetence Mar 11 '21 at 10:50