user8789149
user8789149

Reputation: 121

Firebase upload/download pdf files

Is it possible to manually upload PDF files in Firebase Storage (via Web browser) and then download them via Android application?

Also, do I have to authenticate the user (mobile app) in order to download files?

The idea is to have an "admin" that will upload files and all other users that has a mobile app shouldn't know anything about Firebase, all their app should do is, if there is Internet connection, check if there are new files to download.

Upvotes: 1

Views: 7246

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 599571

do I have to authenticate the user (mobile app) in order to download files?

When you upload a file to Cloud Storage for Firebase, it generates a so-called "download URL" for that file. The download URL is an unguessable URL that gives read-only access to the file. This is perfectly suited for sharing with the users of your Android app.

For an example on how to get the download URL for a file when uploading, have a look at this section of the Firebase documentation.

Upvotes: 1

Yash Verma
Yash Verma

Reputation: 64

Is it possible to manually upload PDF files in Firebase storage (via Web browser) and them download them via Android application?

Yes you can upload PDF files manually using Firebase console. However, to access the file using android application you'd need to know its filename and complete reference.

Also, do I have to authenicate the user (mobile app) in order to download files?

Not necessary, although it's recommended to allow only authenticated access. You can configure this by changing Security Rules in Firebase Storage dashboard. Sample rules:

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
        // CHOOSE EITHER ONE FROM THESE
        allow read, write: if request.auth != null; // requires authentication
        allow read, write: if true; // doesn't require authentication
    }
  }
} 

all their app should do is, if there is Internet connection, check if there are new files to download

Presently, the Firebase storage does not provide an API to get a list of all files stored under a reference. Hence, you cannot directly access the list of files that you've in the bucket.

You can use this method suggested here to use Firebase Realtime Database to store metadata of files(download URL) present in the storage. Note that you'll have to add(remove) enteries to the realtime database as and when you'd upload a file that you want to be listed.

Uploading file to storage:

Uri file = Uri.fromFile(new File("path/to/your_pdf.jpg"));
StorageReference pdfRef = storageRef.child("pdfs/"+file.getLastPathSegment());
UploaduploadTask = pdfRef.putFile(file);

// Register observers to listen for when the download is done or if it fails
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
        Uri downloadUrl = taskSnapshot.getDownloadUrl();

        // ADD TO REALTIME DATABASE TOO.
        addPdfToRealtimeDatabase(downloadUrl);
    }
}); // you might also want to add an onFailure listener.

Downloading code: Set a ChildEventListener to the pdfs in database reference.

ChildEventListener childEventListener = new ChildEventListener() {
     @Override
     public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
         String fileName = dataSnapshot.getKey();
         String downloadUrl = dataSnapshot.getValue(String.class);
         // Add pdf to the display list.
         // displayList contains urls of pdfs to be downloaded.
         displayList.add(downloadUrl);
     }
     // Other methods of ChildEventListener go here
};
pdfRef.addChildEventListener(childEventListener);

You can use the displayList to download all available PDFs and display them.

Note: I've modified the code snippets from firebase documentation.

Upvotes: 1

Related Questions