aws-s3-cg-lib
https://github.com/CloudGenUser/aws-s3-cg-lib
1. Introduction
This code has the objective to establish a connection with the AWS S3 bucket service and depending of the option selected (with the flag property) it will make a different operation in the AWS bucket.
The next are the possible flags and their corresponding actions:
- Get the list of all buckets of the account.
- Create a new bucket in the account.
- Upload a file from a local machine to an specific S3 bucket in the account.
- Get the list of files in a specific bucket.
- Delete a specific bucket inside the account.
- Get the content of a file that is inside a bucket.
- Get the grant of a bucket.
Any other flag will be consider as an invalid values and will return an error message.
The purpose of this library is to handle S3 buckets in the AWS, getting information from the files in the bucket and other operations with the content of the bucket. Also, directories inside the bucket can be handled.
2. Library usage
The library can be installed from npm page with the next:
npm install aws-s3-cg-lib
, npm i aws-s3-cg-lib
or yarn install aws-s3-cg-lib
2.1. CREATETBUCKET
- Arguments:
- Required:
- flag: The string that contains the action to be executed, one of the next options can be selected: CREATETBUCKET, DELETEBUCKET, GETBUCKETGRANTS, GETFILE, GETLISTBUCKETS, GETLISTFILESINBUCKET, UPLOADFILE. The string is not case sensitive. In this case the CREATEBUKET is selected.
- accessKey: It is the key to grant the access to the Amazon services.
- secretKey: It is the password to access with the related key to the Amazon services.
- region: It is a code that corresponds to the zone of the services in wich the Amazon services are alocated according with the user contract to use the AWS services, the list of codes and what is the neares code in user zone can be reviewed in the next link: https://aws.amazon.com/es/about-aws/global-infrastructure/regions_az/
- bucketName: The name of the bucket in which is going to be performed the actions that includes the creation, deletion, to know grants, to search a file, to get the list of files or create the file.
The content and fileName properties are not required for this operation
- Description: This request will create a new bucket inside the AWS S3 account.
- Sample request:
{
"flag":"CREATETBUCKET",
"region":"us-east-1",
"accessKey":"ACCESSKEYSTRING",
"secretKey":"SECRETKEYSTRING",
"bucketName":"bucketGrants"
}
Resultant sample:
{
response: "Bucket created successfully at x location."
}
In case of some error the library will return the corresponding exception in the AWS S3 service.
2.2. DELETEBUCKET
- Arguments:
- Required:
- flag: The string that contains the action to be executed, one of the next options can be selected: CREATETBUCKET, DELETEBUCKET, GETBUCKETGRANTS, GETFILE, GETLISTBUCKETS, GETLISTFILESINBUCKET, UPLOADFILE. The string is not case sensitive. In this case the option DELETEBUCKET is selected.
- accessKey: It is the key to grant the access to the Amazon services.
- secretKey: It is the password to access with the related key to the Amazon services.
- region: It is a code that corresponds to the zone of the services in wich the Amazon services are alocated according with the user contract to use the AWS services, the list of codes and what is the neares code in user zone can be reviewed in the next link: https://aws.amazon.com/es/about-aws/global-infrastructure/regions_az/
- bucketName: The name of the bucket in which is going to be performed the actions that includes the creation, deletion, to know grants, to search a file, to get the list of files or create the file.
The content and fileName properties are not required for this operation
- Description: This request is used to delete one of the buckets inside a AWS S3 account.
- Sample request:
{
"flag":"DELETEBUCKET",
"region":"us-east-1",
"accessKey":"ACCESSKEYSTRING",
"secretKey":"SECRETKEYSTRING",
"bucketName":"bucketGrants"
}
Resultant sample:
{
response:"Bucket bucketTest was successfully removed."
}
In case of some error the library will return the corresponding exception in the AWS S3 service.
2.3. GETBUCKETGRANTS
- Arguments:
- Required:
- flag: The string that contains the action to be executed, one of the next options can be selected: CREATETBUCKET, DELETEBUCKET, GETBUCKETGRANTS, GETFILE, GETLISTBUCKETS, GETLISTFILESINBUCKET, UPLOADFILE. The string is not case sensitive. In this case the option GETBUCKETGRANTS is selected.
- accessKey: It is the key to grant the access to the Amazon services.
- secretKey: It is the password to access with the related key to the Amazon services.
- region: It is a code that corresponds to the zone of the services in wich the Amazon services are alocated according with the user contract to use the AWS services, the list of codes and what is the neares code in user zone can be reviewed in the next link: https://aws.amazon.com/es/about-aws/global-infrastructure/regions_az/
- bucketName: The name of the bucket in which is going to be performed the actions that includes the creation, deletion, to know grants, to search a file, to get the list of files or create the file.
The content and fileName properties are not required for this operation
- Description: This request will show the grants related to the bucket in the AWS S3 account.
- Sample request:
{
"flag":"GETBUCKETGRANTS",
"region":"us-east-1",
"accessKey":"ACCESSKEYSTRING",
"secretKey":"SECRETKEYSTRING",
"bucketName":"bucketGrants"
}
Resultant sample:
{
"grants": [
{
"Grantee": {
"DisplayName": "displayName",
"ID": "1234567890123456789012345678901234567890123456789012345678901234",
"Type": "CanonicalUser"
},
"Permission": "FULL_CONTROL"
}
]
}
In case of some error the library will return the corresponding exception in the AWS S3 service.
2.4. GETFILE
- Arguments:
- Required:
- flag: The string that contains the action to be executed, one of the next options can be selected: CREATETBUCKET, DELETEBUCKET, GETBUCKETGRANTS, GETFILE, GETLISTBUCKETS, GETLISTFILESINBUCKET, UPLOADFILE. The string is not case sensitive. In this case the option GETFILE is selected.
- accessKey: It is the key to grant the access to the Amazon services.
- secretKey: It is the password to access with the related key to the Amazon services.
- region: It is a code that corresponds to the zone of the services in wich the Amazon services are alocated according with the user contract to use the AWS services, the list of codes and what is the neares code in user zone can be reviewed in the next link: https://aws.amazon.com/es/about-aws/global-infrastructure/regions_az/
- bucketName: The name of the bucket in which is going to be performed the actions that includes the creation, deletion, to know grants, to search a file, to get the list of files or create the file.
- fileName: The name of the file to get from the bucket.
The content property is not required for this operation
- Description: This request will get a file (the content) in a bucket inside the AWS S3 account.
- Sample request:
{
"flag":"GETFILE",
"region":"us-east-1",
"accessKey":"ACCESSKEYSTRING",
"secretKey":"SECRETKEYSTRING",
"bucketName":"scdf-cg-bucket",
"fileName":"testFile.txt"
}
Resultant sample:
{
"content": "[{"mailFrom": "test@mail.com", "mailPerson": "john Smith"}]"
}
In case of some error the library will return the corresponding exception in the AWS S3 service.
2.5. GETLISTBUCKETS
- Arguments:
- Required:
- flag: The string that contains the action to be executed, one of the next options can be selected: CREATETBUCKET, DELETEBUCKET, GETBUCKETGRANTS, GETFILE, GETLISTBUCKETS, GETLISTFILESINBUCKET, UPLOADFILE. The string is not case sensitive. In this case the option GETLISTBUCKETS is selected.
- accessKey: It is the key to grant the access to the Amazon services.
- secretKey: It is the password to access with the related key to the Amazon services.
- region: It is a code that corresponds to the zone of the services in wich the Amazon services are alocated according with the user contract to use the AWS services, the list of codes and what is the neares code in user zone can be reviewed in the next link: https://aws.amazon.com/es/about-aws/global-infrastructure/regions_az/
The content, bucket and fileName properties are not required for this operation
- Description: This request will get a JSON with a list of all the buckets inside the AWS S3 account.
- Sample request:
{
"flag":"GETLISTBUCKETS",
"region":"us-east-1",
"accessKey":"ACCESSKEYSTRING",
"secretKey":"SECRETKEYSTRING"
}
Resultant sample:
{
"response": [
{
"Name": "bucket1",
"CreationDate": "2022-01-01T10:10:10.000Z"
},
{
"Name": "bucket2",
"CreationDate": "2022-01-01T10:10:10.000Z"
}
]
}
In case of some error the library will return the corresponding exception in the AWS S3 service.
2.6. GETLISTFILESINBUCKET
- Arguments:
- Required:
- flag: The string that contains the action to be executed, one of the next options can be selected: CREATETBUCKET, DELETEBUCKET, GETBUCKETGRANTS, GETFILE, GETLISTBUCKETS, GETLISTFILESINBUCKET, UPLOADFILE. The string is not case sensitive. In this case the option GETLISTFILESINBUCKET is selected.
- accessKey: It is the key to grant the access to the Amazon services.
- secretKey: It is the password to access with the related key to the Amazon services.
- region: It is a code that corresponds to the zone of the services in wich the Amazon services are alocated according with the user contract to use the AWS services, the list of codes and what is the neares code in user zone can be reviewed in the next link: https://aws.amazon.com/es/about-aws/global-infrastructure/regions_az/
- bucketName: The name of the bucket in which is going to be performed the actions that includes the creation, deletion, to know grants, to search a file, to get the list of files or create the file.
The content and fileName properties are not required for this operation
- Description: This request will get a JSON with a list of all the files inside a bucket in the AWS S3 account.
- Sample request:
{
"flag":"GETLISTFILESINBUCKET",
"region":"us-east-1",
"accessKey":"ACCESSKEYSTRING",
"secretKey":"SECRETKEYSTRING",
"bucketName":"bucketTest"
}
Resultant sample:
{
"response": {
"IsTruncated": false,
"Marker": "",
"Contents": [
{
"Key": "testFile.txt",
"LastModified": "2022-01-01T10:10:22.000Z",
"ETag": "\"e8d78389ed292fbbe91153exb8e0bae4-9\"",
"ChecksumAlgorithm": [],
"Size": 78858823,
"StorageClass": "STANDARD",
"Owner": {
"DisplayName": "xrnadmin_us",
"ID": "efdba54569b1468058a276bcd4b0f662b728f4a9401186dda9357d949e123456"
}
},
{
"Key": "buckets-s3/",
"LastModified": "2022-01-01T10:10:22.000Z",
"ETag": "\"d41d8cd98f00b204e9812345acf8427e\"",
"ChecksumAlgorithm": [],
"Size": 0,
"StorageClass": "STANDARD",
"Owner": {
"DisplayName": "xraadmin_us",
"ID": "efdba14219b4546789a276bcd4b0f662b728f4a9401186dda9357d949e179520"
}
}
],
"Name": "listBucket",
"Prefix": "",
"MaxKeys": 1000,
"CommonPrefixes": []
}
}
In case of some error the library will return the corresponding exception in the AWS S3 service.
2.7. UPLOADFILE
-
Arguments:
- flag: The string that contains the action to be executed, one of the next options can be selected: CREATETBUCKET, DELETEBUCKET, GETBUCKETGRANTS, GETFILE, GETLISTBUCKETS, GETLISTFILESINBUCKET, UPLOADFILE. The string is not case sensitive. In this case the option UPLOADFILE is selected.
- accessKey: It is the key to grant the access to the Amazon services.
- secretKey: It is the password to access with the related key to the Amazon services.
- region: It is a code that corresponds to the zone of the services in wich the Amazon services are alocated according with the user contract to use the AWS services, the list of codes and what is the neares code in user zone can be reviewed in the next link: https://aws.amazon.com/es/about-aws/global-infrastructure/regions_az/
- bucketName: The name of the bucket in which is going to be performed the actions that includes the creation, deletion, to know grants, to search a file, to get the list of files or create the file.
- content: It corresponds to the content of a file when the option to create a file is selected.
- fileName: The name of the file to be created.
-
Description: This request will create a copy of a file inside a specific bucket in the AWS S3 account.
-
Sample request:
{
"flag":"GETLISTFILESINBUCKET",
"region":"us-east-1",
"accessKey":"ACCESSKEYSTRING",
"secretKey":"SECRETKEYSTRING",
"bucketName":"bucketTest",
"content": "Hello World.",
"fileName": "myFile.txt"
}
Resultant sample:
{
response:"File myFile.txt was successfully uploaded in bucketTest."
}
In case of some error the library will return the corresponding exception in the AWS S3 service.