将图片上传到Google Cloud Storage(Java)
我想开发一个Java应用程序(用于PC),可以将任何图片上传到Google Cloud
Storage。尽管我整夜都在寻找解决方案,但是我不知道如何开始。你们中有人有将图像上传到Google Cloud
Storage的经验吗?是否有更好的Google Cloud Storage替代品?
回答:
您可以公开文件,然后Blob的媒体链接可以在任何地方访问。
https://cloud.google.com/storage/docs/access-control/making-data-
public
这是我的解决方案。
try { FileInputStream serviceAccount =
new FileInputStream("firebaseKey/serviceAccountKey.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://xxxxx.firebaseio.com")
.build();
FirebaseApp fireApp = FirebaseApp.initializeApp(options);
StorageClient storageClient = StorageClient.getInstance(fireApp);
InputStream testFile = new FileInputStream(file.getPath());
String blobString = "NEW_FOLDER/" + "FILE_NAME.jpg";
Blob blob = storageClient.bucket("xxxxxx.appspot.com")
.create(blobString, testFile , Bucket.BlobWriteOption.userProject("xxxxxxx"));
blob.getStorage().createAcl(blob.getBlobId(), Acl.of(Acl.User.ofAllUsers(), Acl.Role.READER));
System.out.println(blob.getMediaLink());
} catch (Exception e){
e.printStackTrace();
}
以上是 将图片上传到Google Cloud Storage(Java) 的全部内容, 来源链接: utcz.com/qa/414804.html