-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcs_upload.R
179 lines (128 loc) · 4.13 KB
/
gcs_upload.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
library(tidyverse)
library(fs)
library(googleCloudStorageR)
# check bucket contents
bucket_contents <- gcs_list_objects("capitol-docs")
# delete contents
map(bucket_contents$name, gcs_delete_object, bucket = "capitol-docs")
closeAllConnections()
gc()
my_dir <- "/Users/jeremyallen/Dropbox/Data/capitol-attack"
# list files for upload
my_files <- dir_ls(
path = here::here("defendants"),
glob = "*.pdf",
recurse = TRUE
) %>% unique()
total <- length(my_files)
# gcs_create_bucket(
# "capitol-docs",
# project_id,
# location = "US",
# storageClass = "STANDARD",
# predefinedAcl = "publicRead",
# predefinedDefaultObjectAcl = "bucketOwnerFullControl"
# )
# modify boundary between simple and resumable uploads
# By default the upload_type will be 'simple' if under 5MB, 'resumable' if over 5MB. Use gcs_upload_set_limit to modify this boundary - you may want it smaller on slow connections, higher on faster connections. 'Multipart' upload is used if you provide a object_metadata.
gcs_upload_set_limit(upload_limit = 2500000L)
options(googleAuthR.verbose=2)
mlog <- file("msg.txt", open = "a")
sink(file = "out.txt", append = TRUE, type = "output")
sink(mlog, append = TRUE, type = "message")
#---- ROUND 1: TRY TO UPLOAD ALL FILES ----
write(x = as.character(Sys.time()), file = paste0(my_dir, "/log.txt"), append = TRUE)
# upload
for (i in seq_along(my_files)) {
skip_to_next <- FALSE
#closeAllConnections()
#Sys.sleep(.5)
message("... ", i, " of ", total, " ... trying to upload ", path_file(my_files[i]))
tryCatch(
expr =
{
gcs_upload(
file = my_files[i],
bucket = "capitol-docs",
name = path_file(my_files[i]),
predefinedAcl = "bucketLevel"
)
},
error = function(e) {
message("... Upload seems to have failed for ", i, ":\n")
write(x = paste0(my_files[i], "\n", e), file = paste0(my_dir, "/log.txt"), append = TRUE)
skip_to_next <<- TRUE
}
)
if(skip_to_next) { next }
}
#---- ROUND 2: TRY FAILED FILES AGAIN ----
write(x = as.character(Sys.time()), file = paste0(my_dir, "/log2.txt"), append = TRUE)
my_failed_files <- readr::read_lines("log.txt") %>%
as_tibble() %>%
filter(str_detect(value, "pdf$")) %>%
drop_na() %>%
pull(value)
new_total <- length(my_failed_files)
# upload
for (i in seq_along(my_failed_files)) {
skip_to_next <- FALSE
closeAllConnections()
Sys.sleep(.5)
message("... ", i, " of ", new_total, " ... trying to upload ", path_file(my_failed_files[i]))
tryCatch(
expr =
{
gcs_upload(
file = my_failed_files[i],
bucket = "capitol-docs",
name = path_file(my_failed_files[i]),
predefinedAcl = "bucketLevel"
)
},
error = function(e) {
message("... Upload seems to have failed for ", i, ":\n")
write(x = paste0(my_failed_files[i], "\n", e), file = paste0(my_dir, "/log2.txt"), append = TRUE)
skip_to_next <<- TRUE
}
)
if(skip_to_next) { next }
}
closeAllConnections()
gc()
#---- ROUND 3: TRY FAILED FILES FROM ROUND 2 AGAIN ----
write(x = as.character(Sys.time()), file = paste0(my_dir, "/log3.txt"), append = TRUE)
my_failed_files2 <- readr::read_lines("log2.txt") %>%
as_tibble() %>%
filter(str_detect(value, "pdf$")) %>%
drop_na() %>%
pull(value)
new_total2 <- length(my_failed_files2)
# upload
for (i in seq_along(my_failed_files2)) {
skip_to_next <- FALSE
closeAllConnections()
Sys.sleep(.5)
message("... ", i, " of ", new_total2, " ... trying to upload ", path_file(my_failed_files2[i]))
tryCatch(
expr =
{
gcs_upload(
file = my_failed_files2[i],
bucket = "capitol-docs",
name = path_file(my_failed_files2[i]),
predefinedAcl = "bucketLevel"
)
},
error = function(e) {
message("... Upload seems to have failed for ", i, ":\n")
write(x = paste0(my_failed_files2[i], "\n", e), file = paste0(my_dir, "/log3.txt"), append = TRUE)
skip_to_next <<- TRUE
}
)
if(skip_to_next) { next }
}
sink(NULL, type = "message")
sink(NULL, type = "output")
closeAllConnections()
gc()