可以通过以下步骤实现:
implementation 'com.google.cloud:google-cloud-storage:1.128.0'
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
// 创建GCS客户端
Storage storage = StorageOptions.getDefaultInstance().getService();
// 定义存储桶和对象的名称
String bucketName = "your-bucket-name";
String objectName = "your-object-name";
// 定义本地图像文件的路径
String localImagePath = "/path/to/local/image.jpg";
// 创建BlobId对象
BlobId blobId = BlobId.of(bucketName, objectName);
// 上传图像到GCS
Blob blob = storage.create(blobId, Files.readAllBytes(Paths.get(localImagePath)));
// 打印上传后的图像URL
System.out.println("Uploaded image URL: " + blob.getMediaLink());
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
// 创建GCS客户端
Storage storage = StorageOptions.getDefaultInstance().getService();
// 定义存储桶和对象的名称
String bucketName = "your-bucket-name";
String objectName = "your-object-name";
// 创建BlobId对象
BlobId blobId = BlobId.of(bucketName, objectName);
// 从GCS中检索图像
Blob blob = storage.get(blobId);
// 打印图像的URL
System.out.println("Retrieved image URL: " + blob.getMediaLink());
这样,你就可以在Android应用程序上从Google Cloud Storage上传和检索图像了。
领取专属 10元无门槛券
手把手带您无忧上云