Ros*_*eti 14 java google-cloud-storage google-cloud-platform
我一直在使用以下 Java 方法在 GCS 中设置存储桶通知。
private void setBucketNotification(String bucketName, String topicId) {
List<String> eventType = new ArrayList<>();
eventType.add("OBJECT_FINALIZE");
try {
Notification notification = new Notification();
notification.setTopic(topicId);
notification.setEventTypes(eventType);
notification.setPayloadFormat("JSON_API_V1");
final GoogleCredential googleCredential = GoogleCredential
.fromStream(Objects.requireNonNull(classloader.getResourceAsStream("Key.json")))
.createScoped(Collections.singletonList(StorageScopes.DEVSTORAGE_FULL_CONTROL));
final com.google.api.services.storage.Storage myStorage = new com.google.api.services.storage.Storage.Builder(
new NetHttpTransport(), new JacksonFactory(), googleCredential).build();
Notification v = myStorage.notifications().insert(bucketName, notification).execute();
} catch (IOException e) {
log.error("Caught an IOException {}",e);
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,它一直运行良好,但最近,我收到了关于GoogleCredential课程弃用的投诉,并尝试进行一些研究,希望找到可能的替代品,但找不到任何东西。谁能帮我指出正确的方向?
Ros*_*eti 30
环顾四周后,我设法修复它,使用GoogleCredentials和HttpRequestInitializer。代码变化如下。
final GoogleCredential googleCredential = GoogleCredential
.fromStream(Objects.requireNonNull(classloader.getResourceAsStream("Key.json")))
.createScoped(Collections.singletonList(StorageScopes.DEVSTORAGE_FULL_CONTROL));
final com.google.api.services.storage.Storage myStorage = new com.google.api.services.storage.Storage.Builder(
new NetHttpTransport(), new JacksonFactory(), googleCredential).build();
Run Code Online (Sandbox Code Playgroud)
变成
final GoogleCredentials googleCredentials = serviceAccountCredentials
.createScoped(Collections.singletonList(StorageScopes.DEVSTORAGE_FULL_CONTROL));
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(googleCredentials);
final com.google.api.services.storage.Storage myStorage = new com.google.api.services.storage.Storage.Builder(
new NetHttpTransport(), new JacksonFactory(), requestInitializer).build();
Run Code Online (Sandbox Code Playgroud)
sll*_*pis 10
您可以找到发布在Google APIs Github 存储库 commits上的替代解决方案。
请使用Google Auth Library for Java来处理应用程序默认凭据和其他基于非 OAuth2 的身份验证。
显式凭据加载示例代码:
要从服务帐户 JSON 密钥获取凭据,请使用 GoogleCredentials.fromStream(InputStream) 或 GoogleCredentials.fromStream(InputStream, HttpTransportFactory)。请注意,必须在访问令牌可用之前刷新凭据。
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("/path/to/credentials.json"));
credentials.refreshIfExpired();
AccessToken token = credentials.getAccessToken();
// OR
AccessToken token = credentials.refreshAccessToken();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10603 次 |
| 最近记录: |