-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestApp.java
executable file
·68 lines (51 loc) · 2.3 KB
/
TestApp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.test;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ImpersonatedCredentials;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.HttpMethod;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.Storage.SignUrlOption;
import com.google.cloud.storage.StorageOptions;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@SpringBootApplication
public class TestApp {
@Controller
class HelloworldController {
@RequestMapping(value="/", produces = MediaType.TEXT_PLAIN_VALUE)
@ResponseBody
String hello() {
String bucketName = System.getenv("BUCKET_NAME");
String saEmail = System.getenv("SA_EMAIL");
String objectName = "file.txt";
String signedURLString = "";
GoogleCredentials sourceCredentials;
try {
sourceCredentials = GoogleCredentials.getApplicationDefault();
// you don't need to use impersonation in java, it detects the env and uses it automatically
//ImpersonatedCredentials targetCredentials = ImpersonatedCredentials.create(sourceCredentials, saEmail, null,Arrays.asList("https://www.googleapis.com/auth/devstorage.read_only"), 2);
//Storage storage_service = StorageOptions.newBuilder().setCredentials(targetCredentials).build().getService();
Storage storage_service = StorageOptions.newBuilder().setCredentials(sourceCredentials).build().getService();
BlobId blobId = BlobId.of(bucketName, objectName);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
URL signedUrl = storage_service.signUrl(blobInfo, 600, TimeUnit.SECONDS, SignUrlOption.httpMethod(HttpMethod.GET));
signedURLString = signedUrl.toExternalForm();
} catch (IOException ioex) {
return "Error " + ioex.getMessage();
}
return signedURLString;
}
}
public static void main(String[] args){
SpringApplication.run(TestApp.class, args);
}
}