-1

Is there a solution how to get the link of the photo from firebase storage only java, not android?

I have -

    FileInputStream serviceAccount = new 
    FileInputStream("./serviceAccountKey.json");

    FirebaseOptions options = new FirebaseOptions.Builder()
            .setCredentials(GoogleCredentials.fromStream(serviceAccount))
            .setStorageBucket("g-app-af0af.appspot.com")
            .build();
    FirebaseApp.initializeApp(options);
            Bucket bucket = StorageClient.getInstance().
                   bucket("g-app-af0af.appspot.com");

I can upload photos

    InputStream testFile = new FileInputStream("./12.png");
    String blobString = "test_folder/" + "12.png";

    Blob blob = bucket.create(blobString, testFile, "image/png", 
                Bucket.BlobWriteOption.userProject("g-app-af0af"));

I can get the link but it’s temporary and I need the eternal !!!

    String bucketName = "g-app-af0af.appspot.com";
    String blobName = "test_folder/12.png";
    String keyPath = "./serviceAccountKey.json";
    URL signedUrl = blob.getStorage().signUrl(BlobInfo.newBuilder(bucketName, 
    blobName).build(),
            14, TimeUnit.SECONDS, 
    Storage.SignUrlOption.signWith(ServiceAccountCredentials.fromStream(
                    new FileInputStream(keyPath))));
    System.out.println(signedUrl);

I can delete and download it is good

     Blob blob1 = bucket.getStorage().get(BlobId.of(bucketName, blobName));
     blob1.downloadTo(Paths.get("./zz.png"));
     bucket.getStorage().delete(bucketName, blobName, 
                  Storage.BlobSourceOption.userProject("g-app-af0af"));

But I can't get a permanent url link to the photo from storage, Well, in principle, as an option, specify 100,000 days thanks for the help

So that I can do it))

  • 1
    The question doesn't appear to include any attempt at all to solve the problem. Please edit the question to show what you've tried, and show a specific roadblock you're running into with [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). For more information, please see [How to Ask](https://stackoverflow.com/help/how-to-ask). – Andreas Nov 06 '19 at 05:43

1 Answers1

0

Download URLs are a mechanism that is specific to the Firebase client-side SDKs and are not available in the server-side SDKs.

The closest equivalent on a server-side SDK is a signed URL, which you can get for Java by calling Storage.signUrl. An example from here:

Storage storage = StorageOptions.getDefaultInstance().getService();
String bucketName = "[BUCKET_NAME]";
String blobName = "[BLOB_NAME]";
String keyPath = "[PATH_TO_KEY]";
URL signedUrl = storage.signUrl(BlobInfo.newBuilder(bucketName, blobName).build(), 
        14, TimeUnit.DAYS, SignUrlOption.signWith(ServiceAccountCredentials.fromStream(
        new FileInputStream(keyPath))));

You can simplify this a bit, since the Firebase Admin SDK will pre-create the Storage instance for you.

Also have a look at this similar answer for Node.js: Get Download URL from file uploaded with Cloud Functions for Firebase

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807