[Archive] AWS S3
This library has been archived and is no longer supported. The latest version of the AWS S3 Library can be found here.
The AWS_S3 library can be used to interact with an AWS S3 bucket structure. Using the AWS_S3 library with an accompanied sensor library, for instance, gives the user the ability to save data output from the sensor directly to their personal cloud space. The user should have the following information after setting up a bucket on AWS S3:
- Region: The AWS region that the bucket resides in (ex: ap-northest-2)
- Bucket name: The name of the bucket that the user set up
- Access key ID: An access key id created through AWS's IAM security credentials page
- Secret key: The secret key associated with the created access key id
After the initial setup of the bucket on AWS S3 and obtaining the required information, the user can utilize the AWS_S3 library to perform three main actions: get, put and delete.
During the creation of the AWS_S3 object, the following parameters must be set; region
, bucketName
, accessKeyId
, secretKey
, fileName
, filePath
, and body
.
These parameters can be set within a typical JavaScript object as {key:value} pairs of type string, as follows:
Options
var userOptions = {
region: "ap-northeast-2",
bucketName: "sample-bucket",
accessKeyId: "ACCESSKEYID",
secretKey: "SeCrEtKeY123",
fileName: "SensorData.csv",
filePath: "/",
body: ""
};
Creation
The above userOptions object can be passed into the AWS_S3 constructor in order to set the user's configuration:
var s3 = new AWS_S3(userOptions);
Parameter Functions
In addition to passing in the userOptions object, users can set each of the fields individually using the following methods:
region
:setRegion({value})
bucketName
:setBucketName({value})
accessKeyId
:setAccessKeyId({value})
secretKey
:setSecretKey({value})
fileName
:setFileName({value})
filePath
:setFilePath({value})
body
:setBody({value})
These methods can be chained together: s3Library.setRegion("newRegion").setFileName("newFile.txt").setBody("New File contents")
.
After creating the S3 object and setting its initial parameters, the user is ready to make calls to put()
, get()
, and delete()
. The parameters must be set to access these methods to communicate with S3.
Main S3 Functions
PUT
When calling the put()
function, the S3 library will attempt to upload the contents of the body
parameter to the current bucket at the file name and path set in the parameters.
GET
When calling the get()
function, the S3 library will attempt to download the contents into the body
parameter from the file path and name set in the parameters from the current bucket. If no filename is specified, the whole directory structure of the bucket will be downloaded.
You can check the downloaded content in the log. Please refer to the Log documentation for functions to access the device log.
DELETE
When calling the delete()
function, the S3 library will attempt to delete the file at the file path and name of the current bucket.
Example Script
The following is an example user script after importing the AWS_S3 library alongside an HTS221 sensor library.
The user activates interfaces on the NEQTO IO board and creates a new I2C device object to interact with the HTS221 sensor.
The user creates a buffer called outputFile
which will be a csv type file of comma delimited values for time, temperature and humidity values. The user then takes 10 readings from the HTS221 sensor over 10 second intervals and appends the data to the outputFile
buffer with a new line for each new reading value set.
After finishing the sensor readings, the user creates an S3 object with their region, bucket, access key id, secret key, filename, file path, and the outputFile
buffer as the body parameters. The user then calls the put()
method to upload the csv file to their S3 bucket.
// IMPORTED LIBRARIES
// - HTS221
// - AWS_S3
// Logging setup
log.setLevel(2); //-1:NONE 0:ERROR 1:WARNING 2:DEBUG 3:TRACE
log.printLevel(2); //0:DISABLE 1:LOG 2:CONSOLE 3:BOTH
log.clear();
//Use standard interface of NEQTO IO board
nqEx.enI2CS(true);
nqEx.enI2CL(true);
//I2C Config
var baudrate = 400000;
var i2c = new I2C(1);
i2c.open(baudrate);
var hts = new HTS221(i2c);
hts.init();
var outputFile = "Time,Temperature,Humidity\n";
var NUM_READINGS = 10;
var iteration = 0;
while (iteration < NUM_READINGS) {
var dateNow = new Date();
var tempHumi = hts.getResult();
outputFile += dateNow.toTimeString() + "," + tempHumi.temp + "," + tempHumi.humi + "\n";
setTimeout(10000).wait();
iteration += 1;
}
// S3 User Options
var userOps = {
region: "ap-northeast-2",
bucketName: "bucket_name",
accessKeyId: "ACCESS_KEY_ID",
secretKey: "SECRET_KEY",
fileName: "HTS221.csv",
filePath: "/",
body: outputFile,
};
var s3 = new AWS_S3(userOps);
s3.put();