0
votes

I'm trying to upload a PDF file to Firebase Storage using the Firebase Admin SDK in Eclipse Java. I have been referring to this doc: https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-java . So far, it seemed like I was able to upload the file BUT the pdf content seems to be damaged somehow as the size(as shown in Firebase Storage) is smaller(11 bytes) as compared to the original size(3028 bytes). So I tried to download the file and as it turns out, I am not able to open the downloaded file. I have implemented the following code:

BlobId blobId = BlobId.of(bucket, folder+"/"+blobName);
        System.out.println("Blob Id:"+blobId);
        BlobInfo blobInfo = BlobInfo
                .newBuilder(blobId)
                .setContentType("application/pdf")              
                .build();
        System.out.println("Blob Info:"+blobInfo);

        Blob blob = storage.create(blobInfo, blobName.getBytes(UTF_8));
        System.out.println("BLOB:"+blob);

I'm not really able to get much info about uploading PDF from the Admin side. I'm guessing blobName.getBytes(UTF_8) might be the issue somehow. Any help appreciated!

1

1 Answers

0
votes

Okay, got it working now by adding the file path to an input stream and using .readAllBytes() method to read the file.

String file_path = file.getPath();
InputStream file_input_stream = new FileInputStream(file_path);

BlobId blobId = BlobId.of(bucket, folder+"/"+blobName);
System.out.println("Blob Id:"+blobId);
BlobInfo blobInfo = BlobInfo
            .newBuilder(blobId)
            .setContentType("application/pdf")              
            .build();
Blob blob = storage.create(blobInfo, file_input_stream.readAllBytes());