투케이2K

960. (Android/Java) [유틸 파일] deleteS3File - Aws S3 버킷 저장소 파일 삭제 수행 - AmazonS3 File Delete 본문

Android

960. (Android/Java) [유틸 파일] deleteS3File - Aws S3 버킷 저장소 파일 삭제 수행 - AmazonS3 File Delete

투케이2K 2025. 3. 15. 19:40

[개발 환경 설정]

개발 툴 : AndroidStudio

개발 언어 : Java / Kotlin

 

[소스 코드]

// --------------------------------------------------------------------------------------
[개발 및 테스트 환경]
// --------------------------------------------------------------------------------------

- 언어 : Java / Kotlin

- 개발 툴 : AndroidStudio

- 기술 구분 : Aws / S3 / Bucket

// --------------------------------------------------------------------------------------






// --------------------------------------------------------------------------------------
[사전) 필요 설정 정리] : build.gradle 의존성 부여
// --------------------------------------------------------------------------------------

// [AWS] : [target 31 이상 의존성]
implementation 'com.amazonaws:aws-android-sdk-s3:2.57.0'
implementation 'com.amazonaws:aws-android-sdk-iot:2.57.0'
implementation 'com.amazonaws:aws-android-sdk-mobile-client:2.57.0'


// [AWS] : [target 31 미만 의존성]
implementation 'com.amazonaws:aws-android-sdk-s3:2.16.13'
implementation 'com.amazonaws:aws-android-sdk-iot:2.16.13'
implementation 'com.amazonaws:aws-android-sdk-mobile-client:2.16.13'

// --------------------------------------------------------------------------------------






// --------------------------------------------------------------------------------------
[사전) AmazonS3Client 초기화 방법
// --------------------------------------------------------------------------------------

https://blog.naver.com/kkh0977/223797255512

// --------------------------------------------------------------------------------------





// --------------------------------------------------------------------------------------
[Java : 소스 코드]
// --------------------------------------------------------------------------------------

public Observable<Boolean> deleteS3File(String awsBucket, String awsKey) {
    S_Log._D_(ACTIVITY_NAME + " :: deleteS3File :: S3 특정 버킷 파일 삭제 수행", new String[]{
            "awsBucketName :: " + String.valueOf(awsBucket),
            "awsKey :: " + String.valueOf(awsKey)
    });

    return Observable.create(subscriber -> {

        try {

            if (s3Client != null && mMainCtx != null){

                if (C_Util.stringNotNull(awsBucket) == true && C_Util.stringNotNull(awsKey) == true){

                    new Thread(() -> {

                        try {

                            // TODO [S3 객체 삭제 요청]
                            DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(awsBucket, awsKey);
                            s3Client.deleteObject(deleteObjectRequest);

                            S_Log._W_(ACTIVITY_NAME + " :: deleteS3File :: S3 특정 버킷 파일 삭제 완료", new String[]{
                                    "awsBucketName :: " + String.valueOf(awsBucket),
                                    "awsKey :: " + String.valueOf(awsKey)
                            });

                            if (subscriber != null && subscriber.isDisposed() == false){
                                subscriber.onNext(true);
                                subscriber.onComplete();
                            }

                        } catch (Exception eq) {
                            S_Log._printStackTrace_(null, S_FinalData.LOG_BUG_STATE, null, eq);

                            if (subscriber != null && subscriber.isDisposed() == false){
                                subscriber.onError(new Throwable("[Exception] : " + String.valueOf(eq.getMessage())));
                                subscriber.onComplete();
                            }
                        }

                    }).start();

                }
                else {
                    S_Log._E_(ACTIVITY_NAME + " :: deleteS3File :: S3 특정 버킷 파일 삭제 에러", new String[]{"Error :: input data is null"});

                    if (subscriber != null && subscriber.isDisposed() == false){
                        subscriber.onError(new Throwable("[Error] : input data is null"));
                        subscriber.onComplete();
                    }
                }

            }
            else {
                S_Log._E_(ACTIVITY_NAME + " :: deleteS3File :: S3 특정 버킷 파일 삭제 에러", new String[]{"Error :: s3Client is null"});

                if (subscriber != null && subscriber.isDisposed() == false){
                    subscriber.onError(new Throwable("[Error] : s3Client is null"));
                    subscriber.onComplete();
                }

            }

        } catch (final Exception e){
            S_Log._printStackTrace_(null, S_FinalData.LOG_BUG_STATE, null, e);

            try {
                if (subscriber != null && subscriber.isDisposed() == false){
                    subscriber.onError(new Throwable("[Exception] : " + String.valueOf(e.getMessage())));
                    subscriber.onComplete();
                }
            }
            catch (Exception ex){
                ex.printStackTrace();
            }
        }

    });
}

// --------------------------------------------------------------------------------------





// --------------------------------------------------------------------------------------
[참고 사이트]
// --------------------------------------------------------------------------------------

[S3 (Amazon Simple Storage Service) 버킷 저장소 개념 및 설명 정리]

https://blog.naver.com/kkh0977/223733087281?trackingCode=blog_bloghome_searchlist


[S3 (Amazon Simple Storage Service) 버킷 저장소에 파일 업로드 방법]

https://blog.naver.com/kkh0977/223733106182?trackingCode=blog_bloghome_searchlist


[AccessKey , SecretKey 사용해 Aws S3 버킷 스토리지 AmazonS3Client 초기화 방법]

https://blog.naver.com/kkh0977/223797255512

// --------------------------------------------------------------------------------------
 
반응형
Comments