使用 SDK for SAP 的 Amazon S3 範例 ABAP - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 SDK for SAP 的 Amazon S3 範例 ABAP

下列程式碼範例示範如何使用 AWS SDK for SAP ABAP搭配 Amazon S3 來執行動作和實作常見案例。

基本概念是程式碼範例,示範如何在服務內執行基本操作。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然 動作會示範如何呼叫個別服務函數,但您可以在其相關案例中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

基本概念

以下程式碼範例顯示做法:

  • 建立儲存貯體並上傳檔案到該儲存貯體。

  • 從儲存貯體下載物件。

  • 將物件複製至儲存貯體中的子文件夾。

  • 列出儲存貯體中的物件。

  • 刪除儲存貯體物件和該儲存貯體。

SDK for SAP ABAP
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

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.

動作

下列程式碼範例示範如何使用 CopyObject

SDK for SAP ABAP
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

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.
  • 如需 API 詳細資訊,請參閱 CopyObject for Word Word 參考中的 Word。 AWS SDK SAP ABAP API

下列程式碼範例示範如何使用 CreateBucket

SDK for SAP ABAP
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

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.
  • 如需 API 詳細資訊,請參閱 CreateBucket for Word Word 參考中的 Word。 AWS SDK SAP ABAP API

下列程式碼範例示範如何使用 DeleteBucket

SDK for SAP ABAP
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

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.
  • 如需 API 詳細資訊,請參閱 DeleteBucket for Word Word 參考中的 Word。 AWS SDK SAP ABAP API

下列程式碼範例示範如何使用 DeleteObject

SDK for SAP ABAP
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

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.
  • 如需 API 詳細資訊,請參閱 DeleteObject for Word Word 參考中的 Word。 AWS SDK SAP ABAP API

下列程式碼範例示範如何使用 GetObject

SDK for SAP ABAP
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

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.
  • 如需 API 詳細資訊,請參閱 GetObject for Word Word 參考中的 Word。 AWS SDK SAP ABAP API

下列程式碼範例示範如何使用 ListObjectsV2

SDK for SAP ABAP
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

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.

下列程式碼範例示範如何使用 PutObject

SDK for SAP ABAP
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

"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.
  • 如需 API 詳細資訊,請參閱 PutObject for Word Word 參考中的 Word。 AWS SDK SAP ABAP API