Skip to content

Commit

Permalink
chore(storage): Fix sample storage_generate_signed_post_policy_v4
Browse files Browse the repository at this point in the history
refs: #8402
pr: #8406
  • Loading branch information
quartzmo authored Dec 17, 2020
1 parent 08461b4 commit 3152236
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 18 deletions.
73 changes: 57 additions & 16 deletions google-cloud-storage/samples/acceptance/files_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -474,24 +474,65 @@ def mock_cipher.random_key

assert bucket.file remote_file_name
end
describe "post object" do
require "net/http"
require "uri"
let(:uri) { URI.parse Google::Cloud::Storage::GOOGLEAPIS_URL }
let(:data) { File.expand_path "../../acceptance/data/logo.jpg", __dir__ }

it "generate_signed_post_policy_v4" do
refute bucket.file remote_file_name
post_object = nil
out, _err = capture_io do
post_object = generate_signed_post_policy_v4 bucket_name: bucket.name,
file_name: remote_file_name
end

it "generate_signed_post_policy_v4" do
refute bucket.file remote_file_name
assert_includes out, "<form action='https://storage.googleapis.com/#{bucket.name}/'"
assert_includes out, "<input name='key' value='#{remote_file_name}'"
assert_includes out, "<input name='x-goog-signature'"
assert_includes out, "<input name='x-goog-date'"
assert_includes out, "<input name='x-goog-credential'"
assert_includes out, "<input name='x-goog-algorithm' value='GOOG4-RSA-SHA256'"
assert_includes out, "<input name='policy'"
assert_includes out, "<input name='x-goog-meta-test' value='data'"
assert_includes out, "<input type='file' name='file'/>"
assert_includes out, "<input type='submit' value='Upload File'/>"

assert post_object
expected_keys = [
"key",
"policy",
"x-goog-algorithm",
"x-goog-credential",
"x-goog-date",
"x-goog-meta-test",
"x-goog-signature"
]
assert_equal expected_keys, post_object.fields.keys.sort

form_data = [["file", File.open(data)]]

post_object.fields.each do |key, value|
form_data.push [key, value]
end

out, _err = capture_io do
generate_signed_post_policy_v4 bucket_name: bucket.name,
file_name: remote_file_name
end

assert_includes out, "<form action='https://storage.googleapis.com/#{bucket.name}/'"
assert_includes out, "<input name='key' value='#{remote_file_name}'"
assert_includes out, "<input name='x-goog-signature'"
assert_includes out, "<input name='x-goog-date'"
assert_includes out, "<input name='x-goog-credential'"
assert_includes out, "<input name='x-goog-algorithm' value='GOOG4-RSA-SHA256'"
assert_includes out, "<input name='policy'"
assert_includes out, "<input name='x-goog-meta-test' value='data'"
assert_includes out, "<input type='file' name='file'/>"
http = Net::HTTP.new uri.host, uri.port
http.use_ssl = true
request = Net::HTTP::Post.new post_object.url
request.set_form form_data, "multipart/form-data"

response = http.request request

_(response.code).must_equal "204"
file = bucket.file post_object.fields["key"]
_(file).wont_be :nil?
Tempfile.open ["google-cloud-logo", ".jpg"] do |tmpfile|
tmpfile.binmode
downloaded = file.download tmpfile
_(File.read(downloaded.path, mode: "rb")).must_equal File.read(data, mode: "rb")
end
end
end

it "set_event_based_hold" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,20 @@ def generate_signed_post_policy_v4 bucket_name:, file_name:
bucket = storage.bucket bucket_name
post_object = bucket.generate_signed_post_policy_v4 file_name,
expires: 600,
fields: { "x-goog-meta-test": "data" }
fields: { "x-goog-meta-test" => "data" }

html_form = "<form action='#{post_object.url}' method='POST' enctype='multipart/form-data'>\n"
post_object.fields.each do |name, value|
html_form += " <input name='#{name}' value='#{value}' type='hidden'/>\n"
end
html_form += " <input type='file' name='file'/><br />\n"
html_form += " <input type='submit' value='Upload File' name='submit'/><br />\n"
html_form += " <input type='submit' value='Upload File'/><br />\n"
html_form += "</form>\n"

puts "You can use the following form to upload an object to bucket #{bucket_name} for the next 10 minutes:\n"
puts html_form

post_object
end
# [END storage_generate_signed_post_policy_v4]

Expand Down

0 comments on commit 3152236

Please sign in to comment.