我一直在使用以下 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课程弃用的投诉,并尝试进行一些研究,希望找到可能的替代品,但找不到任何东西。谁能帮我指出正确的方向?
我HikariDataSource用来连接MariaDB数据库。以下类返回一个Connection。
public class DataSource {
private HikariDataSource ds;
// The constructor takes db name as an argument and creates a new datasource for the connection accordingly.
public DataSource(String dbString) {
HikariConfig config = new HikariConfig();
Map map = DbConfigParser.configKeyValue(dbString);
config.setJdbcUrl(String.valueOf(map.get("uri")));
config.setUsername(String.valueOf(map.get("uname")));
config.setPassword(String.valueOf(map.get("pwd")));
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
ds = new HikariDataSource(config);
}
// Returns a Connection to the database
public Connection getConnection() throws SQLException {
return ds.getConnection();
}
// Close the datasource
public …Run Code Online (Sandbox Code Playgroud) -trigger-resource一个函数是否可以包含两个桶gcloud?我尝试了以下部署,但它似乎只监听bucket-2.
gcloud functions deploy my-function \
--entry-point functions.MyFunction \
--runtime java11 \
--memory 512MB \
--trigger-resource gs://bucket-1 \
--trigger-resource gs://bucket-2 \
--trigger-event google.storage.object.finalize \
--allow-unauthenticated \
--region=europe-west1
Run Code Online (Sandbox Code Playgroud)
将不胜感激任何形式的帮助。
我正在尝试使用以下代码从 Google Cloud 生成下载和上传链接,以查看和上传文件:
public class Test {
public static void main(String[] args) throws IOException {
Storage storage = StorageOptions.newBuilder().setCredentials(
ServiceAccountCredentials.fromStream(new FileInputStream("C:/cred/Key.json")))
.build()
.getService();
String filePath = "file/path/";
File file = new File(filePath);
byte[] bytes = Utilities.fileToByteArray(file);
String mimeType = Utilities.getMimeType(bytes);
BlobId blobId = BlobId.of("bucket", file.getName());
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType(mimeType).build();
URL urlGet = storage
.signUrl(BlobInfo.newBuilder("bucket", "object").build(), 1, TimeUnit.HOURS,
SignUrlOption.httpMethod(HttpMethod.GET));
URL urlPut = storage
.signUrl(blobInfo, 1, TimeUnit.DAYS, SignUrlOption.httpMethod(HttpMethod.PUT),
SignUrlOption.withContentType());
System.out.println(urlGet);
System.out.println(urlPut);
}
}
Run Code Online (Sandbox Code Playgroud)
urlGet包含下载链接并urlPut包含上传链接。当我运行该程序时,我得到以下输出:
https://storage.googleapis.com/roshanbucket/jasperviewpdf?GoogleAccessId=myservice@deft-idiom-234709.iam.gserviceaccount.com&Expires=1552986620&Signature=OZl6M4uMkigu6JBMYoDumgs8P4EC%2BtcK44zHMPkG2xzq3hFfsrj8YYRRtajI8diz64vdCX54nct3wuEpXNRwcnzCmq4KdD53%2B8gbERNuttm8K6%2BlZDLGF3wng%2BCSMzghbGbLnYaZRiZbvjCG%2B3ObBUg9ZiY0qRlif9nyGFftsGUF9GGHvHP6HWP51DJOAurGytSjf9SA5HKPOw4e%2B%2BP1LltfI7m3WjWhxwnSYz4lVxcR4eksec7ILTi66jnwu1gxXtqp75XTxLp9vQa6RC4dCPGoTridFQcMqm89TVzf58c8krk7apQCR6TWp2tAWuFr2xJ1U5FwFfiBaoSX4A33rw%3D%3D
https://storage.googleapis.com/roshanbucket/pi.jpg?GoogleAccessId=myservice@deft-idiom-234709.iam.gserviceaccount.com&Expires=1553069420&Signature=YHsGTgXIBum9t5M7U%2F9fdibDvzBKttQGh0jxzbYgJkevQbpOh8gRQYOlHdjT86byobDE5TNEGF5VrGFAtI5rhRGxLw0xqcNT%2BYGfvHxAIfAJXy5ormXxWVnVEnwGMafyVLOtdIY4asa0niFu%2B36eaIqtD5UzsjUY%2F18OW%2FwvjfQmhlmsvJ7qSkfD1Oif5Rv6c%2F67z1zT7gz7rB4gTCG6mLALuRrOIwCPO%2BkyzOxP9PhEJkoB7j446v%2BhE%2F0pt0wM2nJ29%2BK3HRUShhccJzzZ%2BZRxgOXeUL44CsnYlssaTThU%2FztyUbsXWXbs2hroTcFxVVtOp7aGeCUs1qjdJkXaEg%3D%3D
Run Code Online (Sandbox Code Playgroud)
当我单击第一个链接(即下载)时,它会毫无问题地呈现存储桶中的文件,但是当我使用第二个链接将文件从我的计算机上传到 Google …