public class DownloadFileFromPod {
public static void main(String[] args) {
try (KubernetesClient client = new KubernetesClientBuilder().build()) {
// Path Where to copy file to cloud storage
Path downloadToPath = new File("path_of_gcs").toPath();
// Using Kubernetes Client to copy file from pod
client.pods()
.inNamespace("default") // <- Namespace of pod
.withName("quarkus-84dc4885b-tsck6") // <- Name of pod
.file("/deployments/quarkus-1.0.0-runner.jar") // <- Path of file inside pod
.copy(downloadToPath); // <- path where to copy downloaded file
}
}
}
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class UploadObject {
public static void uploadObject(
String projectId, String bucketName, String objectName, String filePath) throws IOException {
// The ID of your GCP project
// String projectId = "your-project-id";
// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";
// The ID of your GCS object
// String objectName = "your-object-name";
// The path to your file to upload
// String filePath = "path/to/your/file"
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
BlobId blobId = BlobId.of(bucketName, objectName);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
// Optional: set a generation-match precondition to avoid potential race
// conditions and data corruptions. The request returns a 412 error if the
// preconditions are not met.
Storage.BlobTargetOption precondition;
if (storage.get(bucketName, objectName) == null) {
// For a target object that does not yet exist, set the DoesNotExist precondition.
// This will cause the request to fail if the object is created before the request runs.
precondition = Storage.BlobTargetOption.doesNotExist();
} else {
// If the destination already exists in your bucket, instead set a generation-match
// precondition. This will cause the request to fail if the existing object's generation
// changes before the request runs.
precondition =
Storage.BlobTargetOption.generationMatch(
storage.get(bucketName, objectName).getGeneration());
}
storage.create(blobInfo, Files.readAllBytes(Paths.get(filePath)), precondition);
System.out.println(
"File " + filePath + " uploaded to bucket " + bucketName + " as " + objectName);
}
}
2条答案
按热度按时间jchrr9hc1#
您可以使用GCSFUSE将GCS存储桶作为卷装载到Kubernetes Pod。
以下是用于安装和卸载GCS铲斗的集装箱生命周期挂钩示例。
参考:MOUNTING GOOGLE CLOUD STORAGE BUCKET TO KUBERNETES POD
要将文件从Pod复制到GCS存储桶,请使用Fabric8 Kubernetes Client,here中对此进行了详细描述。
示例代码:
您也可以使用
Cloud Storage client libraries
以编程方式实现这一点。下面是使用java上传文件的示例
另外,请查看this thread with python示例以了解更多细节。
ubbxdtey2#
是的,你可以,你需要在你的springboot应用程序镜像中安装gsutil,然后在那个容器中作为configmap或secret挂载你的存储令牌。
最后,您可以像这样运行
gsutil
: