forked from ropensci/targets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_gcp.R
102 lines (95 loc) · 1.98 KB
/
utils_gcp.R
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Semi-automated tests of Google Cloud Storage integration live in tests/gcp/. # nolint
# These tests should not be fully automated because they
# automatically create S3 buckets and upload data,
# which could put an unexpected and unfair burden on
# external contributors from the open source community.
# nocov start
gcp_gcs_exists <- function(
key,
bucket = gcp_gcs_bucket(),
version = NULL
) {
tryCatch(
gcp_gcs_head_true(
key = key,
bucket = bucket,
version = version
),
http_404 = function(condition) {
FALSE
}
)
}
# to keep lines shorter
gcp_gcs_bucket <- function() {
googleCloudStorageR::gcs_get_global_bucket()
}
gcp_gcs_head <- function(
key,
bucket = gcp_gcs_bucket(),
version = NULL,
quiet = TRUE
) {
loud <- if_any(quiet, suppressMessages, identity)
loud(
googleCloudStorageR::gcs_get_object(
key,
bucket = bucket,
meta = TRUE,
generation = version
)
)
}
gcp_gcs_head_true <- function(
key,
bucket = gcp_gcs_bucket(),
version = NULL
) {
gcp_gcs_head(
key = key,
bucket = bucket,
version = version
)
TRUE
}
gcp_gcs_download <- function(
file,
key,
bucket = gcp_gcs_bucket(),
version = NULL
) {
googleCloudStorageR::gcs_get_object(
key,
bucket = bucket,
saveToDisk = file,
overwrite = TRUE,
generation = version
)
}
gcp_gcs_upload <- function(
file,
key,
bucket = gcp_gcs_bucket(),
metadata = list(),
predefined_acl = c(
"private", "bucketLevel", "authenticatedRead",
"bucketOwnerFullControl", "bucketOwnerRead",
"projectPrivate", "publicRead", "default")
) {
predefined_acl <- match.arg(predefined_acl)
meta <- NULL
if (length(metadata) > 0) {
meta <- googleCloudStorageR::gcs_metadata_object(
object_name = key,
metadata = metadata
)
}
googleCloudStorageR::gcs_upload(
file,
bucket = bucket,
name = key,
object_metadata = meta,
predefinedAcl = predefined_acl
)
}
# nocov end