From 8b7b7b2509734a11a1fb361370890e9dbafc4cbd Mon Sep 17 00:00:00 2001 From: Martin Zink Date: Tue, 1 Mar 2022 16:38:25 +0100 Subject: [PATCH] Object metadata should be optional in resumable uploads #332 added cpp examples --- examples/cpp/CMakeLists.txt | 19 +++++++++++++++++++ examples/cpp/list_bucket.cpp | 15 +++++++++++++++ examples/cpp/upload.cpp | 20 ++++++++++++++++++++ fakestorage/upload.go | 3 --- 4 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 examples/cpp/CMakeLists.txt create mode 100644 examples/cpp/list_bucket.cpp create mode 100644 examples/cpp/upload.cpp diff --git a/examples/cpp/CMakeLists.txt b/examples/cpp/CMakeLists.txt new file mode 100644 index 0000000000..9a34403e08 --- /dev/null +++ b/examples/cpp/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required (VERSION 3.2) +set(CMAKE_CXX_STANDARD 20) +project (fake-gcs-server-cpp-examples) +include(FetchContent) + +set(GOOGLE_CLOUD_CPP_ENABLE storage CACHE INTERNAL storage-api) +set(GOOGLE_CLOUD_CPP_ENABLE_MACOS_OPENSSL_CHECK OFF CACHE INTERNAL macos-openssl-check) +set(BUILD_TESTING OFF CACHE INTERNAL testing-off) +#TODO replace b03448411402c83082c279b246548d578dee05c7 with the next release which contains it +FetchContent_Declare(google-cloud-cpp + GIT_REPOSITORY git@github.com:googleapis/google-cloud-cpp.git + GIT_TAG b03448411402c83082c279b246548d578dee05c7) +FetchContent_MakeAvailable(google-cloud-cpp) + +add_executable(upload-example upload.cpp) +target_link_libraries(upload-example google-cloud-cpp::storage) + +add_executable(list-bucket-example list_bucket.cpp) +target_link_libraries(list-bucket-example google-cloud-cpp::storage) diff --git a/examples/cpp/list_bucket.cpp b/examples/cpp/list_bucket.cpp new file mode 100644 index 0000000000..5be7be88c5 --- /dev/null +++ b/examples/cpp/list_bucket.cpp @@ -0,0 +1,15 @@ +#include "google/cloud/storage/client.h" +#include + +using namespace google::cloud::storage; + +int main() { + ClientOptions options(oauth2::CreateAnonymousCredentials()); + options.set_endpoint("localhost:4443"); + Client client(options, LimitedErrorCountRetryPolicy(2)); + auto objects = client.ListObjects("my-bucket"); + for (auto& object : objects) { + if (object.ok()) + std::cout << object.value(); + } +} diff --git a/examples/cpp/upload.cpp b/examples/cpp/upload.cpp new file mode 100644 index 0000000000..2f6b2f9497 --- /dev/null +++ b/examples/cpp/upload.cpp @@ -0,0 +1,20 @@ +#include "google/cloud/storage/client.h" +#include + +using namespace google::cloud::storage; + +int main() { + ClientOptions options(oauth2::CreateAnonymousCredentials()); + options.set_endpoint("localhost:4443"); + Client client(options, LimitedErrorCountRetryPolicy(2)); + client.CreateBucket("my-bucket", BucketMetadata()); + auto writer = client.WriteObject("my-bucket", "my-key"); + writer << "hello world"; + writer.Close(); + auto result = writer.metadata(); + if (!result.ok()) { + std::cout << "Upload failed: " << result.status().message(); + } else { + std::cout << "Upload succeeded"; + } +} diff --git a/fakestorage/upload.go b/fakestorage/upload.go index baeea7ede5..80298e6a99 100644 --- a/fakestorage/upload.go +++ b/fakestorage/upload.go @@ -328,9 +328,6 @@ func (s *Server) resumableUpload(bucketName string, r *http.Request) jsonRespons predefinedACL := r.URL.Query().Get("predefinedAcl") contentEncoding := r.URL.Query().Get("contentEncoding") metadata, err := loadMetadata(r.Body) - if err != nil { - return jsonResponse{errorMessage: err.Error()} - } objName := r.URL.Query().Get("name") if objName == "" { objName = metadata.Name