java代码实现上传文件到谷歌云

1、run configuration 添加vm参数:

-Dhttps.proxy=http://googleapis-dev.gcp.cloud.uk.hsbc:3128
-Dhttps.proxyHost=googleapis-dev.gcp.cloud.uk.hsbc
-Dhttps.proxyPort=3128

2、

Storage.BlobTargetOption precondition = Storage.BlobTargetOption.doesNotExist();
File cert = new File(gcsProperties.getCertPath());
if (!cert.exists()) {
log.error("cert json is not exist");
}
Storage storage =StorageOptions.newBuilder()

.setProjectId(gcsProperties.getProjectId())
        .setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream(gcsProperties.getCertPath())))
.build().getService();

BlobId blobId = BlobId.of(gcsProperties.getBucketName(), objectName);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
//上传文件到GCP
Blob result = storage.create(blobInfo, dataBytes, precondition);



@Data
@AllArgsConstructor
public class GcsProperties {
private String bucketName;
private String certPath;
private String projectId;
}
posted @ 2022-12-30 17:46  ivyJ  阅读(270)  评论(0)    收藏  举报