public URL generateSignedUrl(
String fileName) {
Optional<URL> url = Optional.empty();
try {
long startTime = System.currentTimeMillis();
Storage storage = buildService(projectId);
BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of(bucketName, fileName)).build();
url = Optional.of(storage.signUrl(blobInfo, validTime, TimeUnit.MINUTES));
long endTime = System.currentTimeMillis();
long diffOfTime = endTime - startTime;
LOGGER.info("Signed url for filename : {} , time taken : {}", fileName, String.valueOf(diffOfTime));
} catch (Exception e) {
LOGGER.info("Error for file:{},error {} ", fileName, e);
throw new ApplicationException(
"Failed to get file :: " + fileName, ErrorCode.FAILED_DUE_TO_BUCKET);
}
return url.get();
}
private Storage buildService(String googleProjectId) {
StorageOptions.Builder builder = StorageOptions.newBuilder().setProjectId(googleProjectId);
// First check credPath property. If null, checks GOOGLE_APPLICATION_CREDENTIALS
String credPath = //path set from System.getProperty();
if (credPath.length() > 0)
try {
builder.setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream(credPath)));//not able to mock this line
} catch (Exception e) {
LOGGER.error("Error while setting GCP credential file", e);
throw new ApplicationException(
"Failed to create storage service ", ErrorCode.FAILED_DUE_TO_BUCKET);
}
return builder.build().getService();
Not able to mock line in the code as it requires credential json but i don't wont to provide the private key for the bucket and rather want to mock it for credential. Cannot use powermockito due to restriction