Lua
Lua
S3 Upload Binary File from BinData
See more Amazon S3 (new) Examples
Upload a binary file contained in a BinData object to Amazon S3.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local rest = chilkat.Rest.new()
-- Connect to the Amazon AWS REST server.
local bTls = true
local port = 443
local bAutoReconnect = true
-- Make sure to connect to the region where the bucket is located..
local success = rest:connect("s3-us-west-2.amazonaws.com", port, bTls, bAutoReconnect)
-- Provide AWS credentials for the REST call.
local authAws = chilkat.AuthAws.new()
authAws.access_key = "AWS_ACCESS_KEY"
authAws.secret_key = "AWS_SECRET_KEY"
-- Use the correct region..
authAws.region = "us-west-2"
authAws.service_name = "s3"
success = rest:set_auth_aws(authAws)
-- Set the bucket name via the HOST header.
-- In this case, the bucket name is "chilkat.qa".
-- (Also make sure to use the correct region.)
rest.host = "chilkat.qa.s3-us-west-2.amazonaws.com"
-- Load a text file into memory.
local pngData = chilkat.BinData.new()
success = pngData:load_file("qa_data/png/anemone.png")
if not success then
print("Failed to load file from local filesystem.")
return
end
-- Indicate the Content-Type of our upload. (This is optional)
rest:add_header("Content-Type", "image/png")
-- Upload the file to Amazon S3.
local sbResponse = chilkat.StringBuilder.new()
success = rest:full_request_bd("PUT", "/images/sea_creatures/anemone.png", pngData, sbResponse)
if not success then
print(rest.last_error_text)
return
end
-- Did we get a 200 response indicating success?
local statusCode = rest.response_status_code
if statusCode ~= 200 then
print("Error response: " .. sbResponse:get_as_string())
print("Status code: " .. statusCode .. ", Status text: " .. rest.response_status_text)
return
end
print("File successfully uploaded.")