GAE Application needing to create Expiring Signed URL's to Google Cloud Storage

前端 未结 3 1988
半阙折子戏
半阙折子戏 2020-12-09 01:12

I was successfully able to create a stand-alone Java Application that creates Expiring Signed URL\'s to assets sitting in Google Cloud Storage. However, I have been unsucce

3条回答
  •  情话喂你
    2020-12-09 01:29

    1/ Add the google api google-api-services-storage to your dependencies

    2/ Then you need to create ServiceAccountAuthCredentials object from your service account ID and primary key :

    serviceAccountAuthCredentials = AuthCredentials.createFor(resources.getString("authentication.p12.serviceAccountId"), pk);
    

    3/ Finally you generate the signed URL from the ServiceAccountAuthCredentials object and the bucket and file name (no need to generate a String to sign) :

    public String getSignedURL(String bucket, String fileName) throws IOException, GeneralSecurityException {
        BlobId blobId = BlobId.of(bucket, fileName);
        Blob blob = cloudStorageService.get(blobId);
        URL signedURL = blob.signUrl(durationSignedURLAvailable, TimeUnit.MINUTES, com.google.cloud.storage.Storage.SignUrlOption.signWith(serviceAccountAuthCredentials));
        return signedURL.toString();
    }
    

    This works fine for me.

提交回复
热议问题