-1

I am currently storing text on the cloud using

bucket.create(blobName, "hello".getBytes());

and blob name looks like this 1/1/1674/2020-06-02/9998-2-202062

The requirement is to generate pdf file from the data of multiple blobs. We can do this on our backend in java by getting the content from blob path like this

Blob blob=bucket.get("1/1/1674/2020-06-02/9998-2-202062");
new String(blob.getContent())

But we don't want to increase the load on the server by downloading the content first on the server and then send it to the front-end. So we are sending the signURL on the front-end so we can get the content at the front-end(angular 8) by using that URL and create the pdf. We are creating singurl this way.

Blob blob=bucket.get("1/1/1674/2020-06-02/9998-2-202062");
URL url=blob.signUrl(1, TimeUnit.HOURS,SignUrlOption.signWith(
           ServiceAccountCredentials.fromStream(new FileInputStream(jsonfilePath))));

But the issue on front-end is whenever we click the URL on any browser it downloads the file with the blob-name
9998-2-202062 instead of just showing the content.

Is there any way so we can read the data from that cloud URL instead of downloading the content by chrome automatically? And we do not change the browsers setting because it can not be done on the client machine?

yash arora
  • 31
  • 6
  • 1
    Does your blob is a PDF? Is your backend which generate this PDF? If so, can you share the code where you generate the PDF and store it on GCS? If not, can you explain which element create the PDF? – guillaume blaquiere Jun 16 '20 at 08:51
  • We want to download content from that URL(in angular) but whenever we click on that link it goes downloaded with this name as file. – yash arora Jun 19 '20 at 10:57
  • You did answer me on the process to generate the PDF and how it's stored into Cloud Storage. – guillaume blaquiere Jun 19 '20 at 12:19
  • The signUrl method does exactly what you're saying, it generates an URL to the actual resources that will let you download the file. It **will not** show the content of the file, just a download link. It's important that you explain the process of how you're trying to generate the PDF, mainly because if you download the file on the frontend (which is the users PC), how are you planning to generate the PDF on his side? – Emmanuel Jun 23 '20 at 17:32
  • @Emmanuel We have to prepare pdf from the content of multiple sign URL data on frontend but every time when we try to get the data it downloads the file with the content in it. We want the content from sign URL without downloading it as a file. – yash arora Jun 24 '20 at 13:01

1 Answers1

1

The signUrl method only creates a link to the specified resource, you have to implement a way to read it, join the rest of the files you want to add and process them to create your PDF on the client side.

That said, for example you can create a javascript file and use the signed URL in a fetch function to gather all the text from the blobs, I found this example on another stackoverflow answer that might help you:

Promise.all([
  fetch(signedURL1).then(x => x.text()),
  fetch(signedURL2).then(x => x.text())
]).then(([sampleResp, sample2Resp]) => {
  console.log(sampleResp);
  console.log(sample2Resp);
});

Replace the signedURL1 and signedURL2 for the actual signed URLs that you created.

Once that you have the content of your files, you just have to create the PDF, I found a library named jsPDF that could be of your interest.

Emmanuel
  • 1,436
  • 1
  • 11
  • 17