Amazon S3 examples using SDK for SAP ABAP
The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for SAP ABAP with Amazon S3.
Basics are code examples that show you how to perform the essential operations within a service.
Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.
Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.
Basics
The following code example shows how to:
Create a bucket and upload a file to it.
Download an object from a bucket.
Copy an object to a subfolder in a bucket.
List the objects in a bucket.
Delete the bucket objects and the bucket.
- SDK for SAP ABAP
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. DATA(lo_session) = /aws1/cl_rt_session_aws=>create( cv_pfl ). DATA(lo_s3) = /aws1/cl_s3_factory=>create( lo_session ). " Create an Amazon Simple Storage Service (Amazon S3) bucket. " TRY. lo_s3->createbucket( iv_bucket = iv_bucket_name ). MESSAGE 'S3 bucket created.' TYPE 'I'. CATCH /aws1/cx_s3_bucketalrdyexists. MESSAGE 'Bucket name already exists.' TYPE 'E'. CATCH /aws1/cx_s3_bktalrdyownedbyyou. MESSAGE 'Bucket already exists and is owned by you.' TYPE 'E'. ENDTRY. "Upload an object to an S3 bucket." TRY. "Get contents of file from application server." DATA lv_file_content TYPE xstring. OPEN DATASET iv_key FOR INPUT IN BINARY MODE. READ DATASET iv_key INTO lv_file_content. CLOSE DATASET iv_key. lo_s3->putobject( iv_bucket = iv_bucket_name iv_key = iv_key iv_body = lv_file_content ). MESSAGE 'Object uploaded to S3 bucket.' TYPE 'I'. CATCH /aws1/cx_s3_nosuchbucket. MESSAGE 'Bucket does not exist.' TYPE 'E'. ENDTRY. " Get an object from a bucket. " TRY. DATA(lo_result) = lo_s3->getobject( iv_bucket = iv_bucket_name iv_key = iv_key ). DATA(lv_object_data) = lo_result->get_body( ). MESSAGE 'Object retrieved from S3 bucket.' TYPE 'I'. CATCH /aws1/cx_s3_nosuchbucket. MESSAGE 'Bucket does not exist.' TYPE 'E'. CATCH /aws1/cx_s3_nosuchkey. MESSAGE 'Object key does not exist.' TYPE 'E'. ENDTRY. " Copy an object to a subfolder in a bucket. " TRY. lo_s3->copyobject( iv_bucket = iv_bucket_name iv_key = |{ iv_copy_to_folder }/{ iv_key }| iv_copysource = |{ iv_bucket_name }/{ iv_key }| ). MESSAGE 'Object copied to a subfolder.' TYPE 'I'. CATCH /aws1/cx_s3_nosuchbucket. MESSAGE 'Bucket does not exist.' TYPE 'E'. CATCH /aws1/cx_s3_nosuchkey. MESSAGE 'Object key does not exist.' TYPE 'E'. ENDTRY. " List objects in the bucket. " TRY. DATA(lo_list) = lo_s3->listobjects( iv_bucket = iv_bucket_name ). MESSAGE 'Retrieved list of objects in S3 bucket.' TYPE 'I'. CATCH /aws1/cx_s3_nosuchbucket. MESSAGE 'Bucket does not exist.' TYPE 'E'. ENDTRY. DATA text TYPE string VALUE 'Object List - '. DATA lv_object_key TYPE /aws1/s3_objectkey. LOOP AT lo_list->get_contents( ) INTO DATA(lo_object). lv_object_key = lo_object->get_key( ). CONCATENATE lv_object_key ', ' INTO text. ENDLOOP. MESSAGE text TYPE'I'. " Delete the objects in a bucket. " TRY. lo_s3->deleteobject( iv_bucket = iv_bucket_name iv_key = iv_key ). lo_s3->deleteobject( iv_bucket = iv_bucket_name iv_key = |{ iv_copy_to_folder }/{ iv_key }| ). MESSAGE 'Objects deleted from S3 bucket.' TYPE 'I'. CATCH /aws1/cx_s3_nosuchbucket. MESSAGE 'Bucket does not exist.' TYPE 'E'. ENDTRY. " Delete the bucket. " TRY. lo_s3->deletebucket( iv_bucket = iv_bucket_name ). MESSAGE 'Deleted S3 bucket.' TYPE 'I'. CATCH /aws1/cx_s3_nosuchbucket. MESSAGE 'Bucket does not exist.' TYPE 'E'. ENDTRY.
-
For API details, see the following topics in AWS SDK for SAP ABAP API reference.
-
Actions
The following code example shows how to use CopyObject
.
- SDK for SAP ABAP
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. TRY. lo_s3->copyobject( iv_bucket = iv_dest_bucket iv_key = iv_dest_object iv_copysource = |{ iv_src_bucket }/{ iv_src_object }| ). MESSAGE 'Object copied to another bucket.' TYPE 'I'. CATCH /aws1/cx_s3_nosuchbucket. MESSAGE 'Bucket does not exist.' TYPE 'E'. CATCH /aws1/cx_s3_nosuchkey. MESSAGE 'Object key does not exist.' TYPE 'E'. ENDTRY.
-
For API details, see CopyObject in AWS SDK for SAP ABAP API reference.
-
The following code example shows how to use CreateBucket
.
- SDK for SAP ABAP
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. TRY. lo_s3->createbucket( iv_bucket = iv_bucket_name ). MESSAGE 'S3 bucket created.' TYPE 'I'. CATCH /aws1/cx_s3_bucketalrdyexists. MESSAGE 'Bucket name already exists.' TYPE 'E'. CATCH /aws1/cx_s3_bktalrdyownedbyyou. MESSAGE 'Bucket already exists and is owned by you.' TYPE 'E'. ENDTRY.
-
For API details, see CreateBucket in AWS SDK for SAP ABAP API reference.
-
The following code example shows how to use DeleteBucket
.
- SDK for SAP ABAP
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. TRY. lo_s3->deletebucket( iv_bucket = iv_bucket_name ). MESSAGE 'Deleted S3 bucket.' TYPE 'I'. CATCH /aws1/cx_s3_nosuchbucket. MESSAGE 'Bucket does not exist.' TYPE 'E'. ENDTRY.
-
For API details, see DeleteBucket in AWS SDK for SAP ABAP API reference.
-
The following code example shows how to use DeleteObject
.
- SDK for SAP ABAP
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. TRY. lo_s3->deleteobject( iv_bucket = iv_bucket_name iv_key = iv_object_key ). MESSAGE 'Object deleted from S3 bucket.' TYPE 'I'. CATCH /aws1/cx_s3_nosuchbucket. MESSAGE 'Bucket does not exist.' TYPE 'E'. ENDTRY.
-
For API details, see DeleteObject in AWS SDK for SAP ABAP API reference.
-
The following code example shows how to use GetObject
.
- SDK for SAP ABAP
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. TRY. oo_result = lo_s3->getobject( " oo_result is returned for testing purposes. " iv_bucket = iv_bucket_name iv_key = iv_object_key ). DATA(lv_object_data) = oo_result->get_body( ). MESSAGE 'Object retrieved from S3 bucket.' TYPE 'I'. CATCH /aws1/cx_s3_nosuchbucket. MESSAGE 'Bucket does not exist.' TYPE 'E'. CATCH /aws1/cx_s3_nosuchkey. MESSAGE 'Object key does not exist.' TYPE 'E'. ENDTRY.
-
For API details, see GetObject in AWS SDK for SAP ABAP API reference.
-
The following code example shows how to use ListObjectsV2
.
- SDK for SAP ABAP
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. TRY. oo_result = lo_s3->listobjectsv2( " oo_result is returned for testing purposes. " iv_bucket = iv_bucket_name ). MESSAGE 'Retrieved list of objects in S3 bucket.' TYPE 'I'. CATCH /aws1/cx_s3_nosuchbucket. MESSAGE 'Bucket does not exist.' TYPE 'E'. ENDTRY.
-
For API details, see ListObjectsV2 in AWS SDK for SAP ABAP API reference.
-
The following code example shows how to use PutObject
.
- SDK for SAP ABAP
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. "Get contents of file from application server." DATA lv_body TYPE xstring. OPEN DATASET iv_file_name FOR INPUT IN BINARY MODE. READ DATASET iv_file_name INTO lv_body. CLOSE DATASET iv_file_name. "Upload/put an object to an S3 bucket." TRY. lo_s3->putobject( iv_bucket = iv_bucket_name iv_key = iv_file_name iv_body = lv_body ). MESSAGE 'Object uploaded to S3 bucket.' TYPE 'I'. CATCH /aws1/cx_s3_nosuchbucket. MESSAGE 'Bucket does not exist.' TYPE 'E'. ENDTRY.
-
For API details, see PutObject in AWS SDK for SAP ABAP API reference.
-