This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
storage-api.R
184 lines (160 loc) · 4.56 KB
/
storage-api.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#' List storage containers from Azure Storage.
#'
#' @param prefix Filters the results to return only containers
#' whose name begins with the specified prefix.
#'
#' @examples
#' \dontrun{
#' containers <- listStorageContainers()
#' View(containers)
#' }
#' @export
listStorageContainers <- function(prefix = "") {
config <- getConfiguration()
storageClient <- config$storageClient
xmlResponse <-
storageClient$containerOperations$listContainers(
prefix, content = "parsed")
name <- getXmlValues(xmlResponse, ".//Container/Name")
lastModified <-
getXmlValues(xmlResponse, ".//Container/Properties/Last-Modified")
publicAccess <-
getXmlValues(xmlResponse, ".//Container/Properties/PublicAccess")
leaseState <-
getXmlValues(xmlResponse, ".//Container/Properties/LeaseState")
data.frame(
Name = name,
PublicAccess = publicAccess,
LeaseState = leaseState,
LastModified = lastModified
)
}
#' Delete a storage container from Azure Storage
#'
#' @param container The name of the container
#'
#' @export
deleteStorageContainer <- function(container, verbose = TRUE) {
config <- getConfiguration()
storageClient <- config$storageClient
response <-
storageClient$containerOperations$deleteContainer(container, content = "response")
tryCatch({
httr::stop_for_status(response)
if (verbose) {
cat(sprintf("Your storage container '%s' has been deleted.", jobId),
fill = TRUE)
}
},
error = function(e) {
# Checking for status code instead of using xml2 package
# Storage helper functions require xml2 package which requires special installations
if (verbose && response$status_code == 404) {
cat(sprintf("Call: deleteStorageContainer"),
fill = TRUE)
cat(sprintf("Exception: %s", "The specified storage container does not exist"),
fill = TRUE)
}
}
)
}
#' List storage files from Azure storage.
#'
#' @param container The cluster object
#' @param prefix Id of the node
#'
#' @examples
#' \dontrun{
#' files <- listStorageFiles("job001")
#' View(files)
#' }
#' @export
listStorageFiles <- function(container, prefix = "", ...) {
config <- getConfiguration()
storageClient <- config$storageClient
xmlResponse <- storageClient$blobOperations$listBlobs(
container,
prefix,
content = "parsed",
...)
filePath <- getXmlValues(xmlResponse, ".//Blob/Name")
lastModified <-
getXmlValues(xmlResponse, ".//Blob/Properties/Last-Modified")
contentLength <-
getXmlValues(xmlResponse, ".//Blob/Properties/Content-Length")
contentType <-
getXmlValues(xmlResponse, ".//Blob/Properties/Content-Type")
leaseState <-
getXmlValues(xmlResponse, ".//Blob/Properties/LeaseState")
storageFiles <- data.frame(
FilePath = filePath,
ContentLength = contentLength,
ContentType = contentType,
LeaseState = leaseState,
LastModified = lastModified
)
attr(storageFiles, "containerName") <- container
storageFiles
}
#' Get a storage file from Azure Storage. By default, this operation will print the files on screen.
#'
#' @param container The name of the container
#' @param blobPath The path of the blob
#' @param ... Optional parameters
#' \itemize{
#' \item{"downloadPath"}: { Path to save file to }
#' \item{"overwrite"}: { Will only overwrite existing localPath }
#' \item{"verbose"}: { Show verbose messages }
#'}
#' @examples
#' \dontrun{
#' stdoutText <- getStorageFile(testContainer, "logs/stdout.txt")
#' }
#' @export
getStorageFile <-
function(container,
blobPath,
downloadPath = NULL,
overwrite = FALSE,
verbose = TRUE,
...) {
config <- getConfiguration()
storageClient <- config$storageClient
jobFileContent <-
storageClient$blobOperations$downloadBlob(
container,
blobPath,
downloadPath = downloadPath,
overwrite = overwrite,
progress = TRUE,
...
)
jobFileContent
}
#' Delete a storage file from a container.
#'
#' @param container The name of container
#' @param blobPath The file path of the blob
#'
#' @export
deleteStorageFile <- function(container, blobPath, ...) {
config <- getConfiguration()
storageClient <- config$storageClient
response <-
storageClient$blobOperations$deleteBlob(
container,
blobPath,
content = "response",
...)
if (response$status_code == 202) {
cat(
sprintf(
"Your blob '%s' from container '%s' has been deleted.",
blobPath,
container
),
fill = TRUE
)
}
response
}