本地文件上传GCP桶
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
public class GCSUploader {
public static void main(String[] args) {
String projectId = "your-project-id";
String bucketName = "your-bucket-name";
String localFolderPath = "/path/to/local/folder";
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
File folder = new File(localFolderPath);
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile()) {
String destinationPath = "folder/" + file.getName();
BlobInfo blobInfo = BlobInfo.newBuilder(bucketName, destinationPath).build();
Path localFilePath = Paths.get(file.getAbsolutePath());
Blob blob = storage.create(blobInfo, localFilePath);
System.out.println("文件上传成功:" + blob.getName());
}
}
}
}
}