This repository was archived by the owner on Apr 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_upload.rb
More file actions
190 lines (169 loc) · 5.63 KB
/
file_upload.rb
File metadata and controls
190 lines (169 loc) · 5.63 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
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
185
186
187
188
189
190
#!/usr/bin/env ruby
# frozen_string_literal: true
# ==================================================================
# A Ruby example that demonstrates the workflow for uploading a file
# to Sentera's cloud storage using a single PUT operation, and then
# attaching the file to something like a field, survey, feature set,
# mosaic, etc.
#
# Full documentation of this workflow can be found here:
# https://api.sentera.com/api/getting_started/uploading_files.html
#
# Contact support@sentera.com with any questions.
# ==================================================================
require 'net/http'
require 'json'
require 'digest'
require './utils'
# If you want to debug this script, run the following gem install
# commands. Then uncomment the require statements below, and put
# debugger statements in the code to trace the code execution.
#
# > gem install pry
# > gem install pry-byebug
#
# require 'pry'
# require 'pry-byebug'
#
# This method demonstrates how to use the create_file_upload
# mutation in Sentera's GraphQL API to prepare a file for
# upload to Sentera's cloud storage.
#
# @param [string] file_path Fully qualified path to file to upload
# @param [string] content_type MIME content type of the file
#
# @return [Hash] Hash containing results of the GraphQL request
#
def create_file_upload(file_path, content_type)
puts 'Create file upload'
filename = File.basename(file_path)
byte_size = File.size(file_path)
checksum = Digest::MD5.base64digest(File.read(file_path))
gql = <<~GQL
mutation CreateFileUpload(
$byte_size: BigInt!
$checksum: String!
$content_type: String!
$filename: String!
) {
create_file_upload(
filename: $filename
content_type: $content_type
byte_size: $byte_size
checksum: $checksum
) {
id
url
headers
}
}
GQL
variables = {
byte_size: byte_size,
checksum: checksum,
content_type: content_type,
filename: filename
}
response = make_graphql_request(gql, variables)
json = JSON.parse(response.body)
json.dig('data', 'create_file_upload')
end
#
# This method demonstrates how to upload a file to
# Sentera's cloud storage using the URL and headers
# that were retrieved via the create_file_upload
# GraphQL mutation.
#
# @param [string] url Pre-signed URL used to upload the file
# @param [Hash] headers Hash of headers used on the request to
# PUT the file to the specified URL
# @param [string] file_path Fully qualified path to file to upload
#
# @return [void]
#
def upload_file(url, headers, file_path)
puts 'Upload file'
uri = URI(url)
file_contents = File.read(file_path)
Net::HTTP.start(uri.host) do |http|
response = http.send_request('PUT',
uri,
file_contents,
headers)
puts "upload_file response.code = #{response.code}"
end
end
#
# This method demonstrates how to use the ID of a file that
# was previously uploaded to Sentera's cloud storage with one
# of the mutations in Sentera's GraphQL API that accepts a
# file ID as an input. In this example, we'll use the
# import_feature_set GraphQL mutation to attach the file to
# a feature set owned by the specified file owner
#
# @param [string] file_id ID of the uploaded file
# @param [string] owner_type Type of owner that will own the
# feature set. For example: SURVEY.
# @param [string] owner_sentera_id Sentera ID of the resource
# (field, survey, feature set, etc.)
# that will own the feature set that
# is created.
#
# @return [Hash] Hash containing results of the GraphQL request
#
def import_feature_set(file_id, owner_type, owner_sentera_id)
puts 'Use file'
gql = <<~GQL
mutation ImportFeatureSet(
$geometry_file_key: FileKey
$name: String!
$owner_sentera_id: ID!
$owner_type: FeatureSetOwnerType!
$type: FeatureSetType!
) {
import_feature_set(
geometry_file_key: $geometry_file_key
name: $name
owner_sentera_id: $owner_sentera_id
owner_type: $owner_type
type: $type
) {
status
}
}
GQL
variables = {
geometry_file_key: file_id,
name: 'Test Feature Set',
owner_sentera_id: owner_sentera_id,
owner_type: owner_type,
type: 'UNKNOWN'
}
response = make_graphql_request(gql, variables)
json = JSON.parse(response.body)
json.dig('data', 'import_feature_set')
end
# MAIN
# **************************************************
# Set these variables based on the file you want to
# upload and the resource within FieldAgent to which
# you wish to attach the file.
file_path = ENV.fetch('FILE_PATH', 'test.geojson') # Your fully qualified file path
content_type = ENV.fetch('CONTENT_TYPE', 'application/json') # Your MIME content type
owner_type = ENV.fetch('OWNER_TYPE', 'SURVEY') # Your owner type
owner_sentera_id = ENV.fetch('OWNER_SENTERA_ID', 'sezjmpa_CO_arpmAcmeOrg_CV_deve_b822f1701_230330_110124') # Your owner Sentera ID
# **************************************************
# Step 1: Create a file upload
results = create_file_upload(file_path, content_type)
upload_url = results['url']
upload_headers = results['headers']
file_id = results['id']
# Step 2: Upload the file
upload_file(upload_url, upload_headers, file_path)
# Step 3: Use the file with FieldAgent
results = import_feature_set(file_id, owner_type, owner_sentera_id)
if results
puts "Done! File #{file_path} was successfully imported to a feature set attached to #{owner_type} #{owner_sentera_id}."
else
puts 'Failed'
end