-
Notifications
You must be signed in to change notification settings - Fork 469
Expand file tree
/
Copy pathstorage_quickstart.cc
More file actions
86 lines (72 loc) · 2.89 KB
/
Copy pathstorage_quickstart.cc
File metadata and controls
86 lines (72 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "google/cloud/storage/examples/storage_examples_common.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/internal/random.h"
// [START storage_quickstart]
#include "google/cloud/storage/client.h"
#include <iostream>
#include <random>
#include <string>
#include <utility>
#include <vector>
// [END storage_quickstart]
namespace {
using ::google::cloud::storage::examples::Usage;
// [START storage_quickstart]
void StorageQuickstart(std::string const& bucket_name) {
// Create an aliases to make the code easier to read.
namespace gcs = ::google::cloud::storage;
// Create a client to communicate with Google Cloud Storage. This client
// uses the default configuration for authentication and project id.
auto client = gcs::Client();
// Create a bucket
google::cloud::StatusOr<gcs::BucketMetadata> metadata = client.CreateBucket(
bucket_name, gcs::BucketMetadata().set_location("US").set_storage_class(
gcs::storage_class::Standard()));
if (!metadata) throw std::move(metadata).status();
std::cout << "Created bucket " << metadata->name() << "\n";
}
// [END storage_quickstart]
void StorageQuickstartCommand(std::vector<std::string> const& argv) {
if (argv.size() != 1 || argv[0] == "--help") {
throw Usage{"storage-quickstart <bucket-name>"};
}
StorageQuickstart(argv[0]);
}
void RunAll(std::vector<std::string> const& argv) {
namespace examples = ::google::cloud::storage::examples;
namespace gcs = ::google::cloud::storage;
if (!argv.empty()) throw examples::Usage{"auto"};
examples::CheckEnvironmentVariablesAreSet({
"GOOGLE_CLOUD_PROJECT",
});
auto const project_id =
google::cloud::internal::GetEnv("GOOGLE_CLOUD_PROJECT").value();
auto generator = google::cloud::internal::DefaultPRNG(std::random_device{}());
auto const bucket_name = examples::MakeRandomBucketName(generator);
std::cout << "\nRunning StorageQuickStart() example" << std::endl;
StorageQuickstartCommand({bucket_name});
auto client = gcs::Client();
(void)examples::RemoveBucketAndContents(client, bucket_name);
}
} // namespace
int main(int argc, char* argv[]) {
namespace examples = ::google::cloud::storage::examples;
examples::Example example({
{"storage-quickstart", StorageQuickstartCommand},
{"auto", RunAll},
});
return example.Run(argc, argv);
}