투케이2K

644. (ios/swift5) [Soto 7.2.0] AWS S3 PutObjectRequest 특정 파일 업로드 수행 본문

IOS

644. (ios/swift5) [Soto 7.2.0] AWS S3 PutObjectRequest 특정 파일 업로드 수행

투케이2K 2025. 7. 6. 08:39
728x90
반응형

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : SWIFT5

 

[소스 코드]

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

- 언어 : Swift5

- 개발 툴 : Xcode

- 기술 구분 : Soto / AWS / S3

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






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

import Foundation
import UIKit

import SotoCore
import SotoS3
import NIO
import NIOCore

class C_Aws_S3_Storage_Module: NSObject {
    
    
    // -----------------------------------------------------------------------------------------
    // MARK: - [전역 변수 선언]
    // -----------------------------------------------------------------------------------------
    public static let ACTIVITY_NAME = "C_Aws_S3_Storage_Module"
    
    public static let AWS_IAM_CLI_ACCESS_KEY = "AK..7Q"; 

    public static let AWS_IAM_CLI_SECRET_KEY = "Zz..xj"; 
    
    public static let AWS_IAM_CLI_REGION = "ap-northeast-2"; // TODO [서울 리전]
    
    public static let AWS_BUCKET_NAME = "service"; // TODO [버킷 이름]
    
    public static let AWS_BUCKET_KEY = "control/private.txt"; // TODO [버킷 Key]
    
    
    
    
    
    
    // -----------------------------------------------------------------------------------------
    // MARK: - [SEARCH FAST] : getS3FileUpload : S3 특정 파일 업로드 수행
    // -----------------------------------------------------------------------------------------
    // TODO [Call Method]
    // ------------------------------------------------------------------------------------------
    /*
     if let fileURL = Bundle.main.url(forResource: "test", withExtension: "txt") {
         
         C_Aws_S3_Storage_Module().getS3FileUpload(accessKey: C_Aws_S3_Storage_Module.AWS_IAM_CLI_ACCESS_KEY, secretKey: C_Aws_S3_Storage_Module.AWS_IAM_CLI_SECRET_KEY, region: C_Aws_S3_Storage_Module.AWS_IAM_CLI_REGION, bucketName: C_Aws_S3_Storage_Module.AWS_BUCKET_NAME, bucketKey: "wcpcontrol/iosTest.txt", fileUrl: fileURL){(result) in
                 
                 S_Log._F_(description: "AWS S3 특정 파일 업로드 결과", data: ["\(result)"])
                 
         }
         
     } else {
         S_Log._E_(description: "AWS S3 특정 파일 업로드 실패 : 프로젝트 내에 포함 된 파일 Bundle.main.url 을 찾을 수 없습니다", data: nil)
     }
    */
    // ------------------------------------------------------------------------------------------
    func getS3FileUpload(accessKey: String, secretKey: String, region: String, bucketName: String, bucketKey: String, fileUrl: URL, completion: @escaping (Bool)->()) {
        
        /*
        // -----------------------------------------
        [getS3FileUpload 메소드 설명]
        // -----------------------------------------
        1. S3 특정 파일 업로드 수행
        // -----------------------------------------
        2. 필요 import : package(url: "https://github.com/soto-project/soto.git", from: "7.2.0")
         
         import SotoCore
         import SotoS3
         import NIO
         import NIOCore
        // -----------------------------------------
        3. 참고 사항 :
         
         AWS IAM 계정의 AccessKeyId 와 SecretAccessKey 를 가지고 있어야합니다

         해당 계정이 sts:AssumeRole 권한을 가지고 있어야합니다
        // -----------------------------------------
        */

        
        // [로직 처리 수행]
        DispatchQueue.main.async {
            
            Task { // MARK: await used
                
                S_Log._D_(description: "S3 특정 파일 업로드 수행 요청", data: [
                    "accessKey :: \(accessKey)",
                    "secretKey :: \(secretKey)",
                    "region :: \(region)",
                    "bucketName :: \(bucketName)",
                    "bucketKey :: \(bucketKey)",
                    "fileUrl :: \(fileUrl.absoluteString)"
                ])
                
                
                // [방어 로직 : input data check]

                if C_Util().stringNotNullMulti(data: [accessKey, secretKey, region, bucketName, bucketKey, fileUrl.absoluteString]) == true {
                    
                    // ---------------------------------------------
                    // MARK: 자격 증명 직접 입력 (정적 방식)
                    // ---------------------------------------------
                    let client = AWSClient(
                        credentialProvider: .static(
                            accessKeyId: accessKey,
                            secretAccessKey: secretKey
                        )
                        //, httpClientProvider: .createNew // SDK 6.8.0 이하 버전 설정 필요
                    )
                    
                    do {
                        
                        
                        // ---------------------------------------------
                        // MARK: S3 서비스 인스턴스 생성
                        // ---------------------------------------------
                        let s3 = S3(client: client, region: Region(rawValue: region))
                        
                        
                        // ---------------------------------------------
                        // MARK: 파일 읽기 및 ByteBuffer 변환
                        // ---------------------------------------------
                        let fileData = try Data(contentsOf: fileUrl)
                        
                        var buffer = ByteBufferAllocator().buffer(capacity: fileData.count)
                        buffer.writeBytes(fileData)
                        
                        
                        // ---------------------------------------------
                        // MARK: PutObject 요청
                        // ---------------------------------------------
                        let request = S3.PutObjectRequest(
                            body: AWSHTTPBody(buffer: buffer),
                            bucket: bucketName,
                            contentLength: Int64(fileData.count), // key보다 앞에 위치해야 함
                            contentType: "application/octet-stream", // 또는 "image/png", "text/plain" 등
                            key: bucketKey
                        )
                        
                        
                        // ---------------------------------------------
                        // MARK: 업로드 수행 실시
                        // ---------------------------------------------
                        let response = try await s3.putObject(request)
                        
                        S_Log._W_(description: C_Aws_S3_Storage_Module.ACTIVITY_NAME + " :: getS3FileUpload :: Http End", data: nil )
                        
                        
                        // ---------------------------------------------
                        // MARK: 업로드 성공 상태 체크
                        // ---------------------------------------------
                        let eTag = String(describing: response.eTag?.description ?? "") // 엔터티 태그 값
                        
                        if C_Util().stringNotNull(str: eTag) == true {
                            S_Log._W_(description: C_Aws_S3_Storage_Module.ACTIVITY_NAME + " :: getS3FileUpload :: Success", data: [ eTag ] )
                            
                            completion(true) // [Return CallBack]
                        }
                        else {
                            S_Log._E_(description: C_Aws_S3_Storage_Module.ACTIVITY_NAME + " :: getS3FileUpload :: Error", data: [ "eTag Is Null" ] )
                            
                            completion(false) // [Return CallBack]
                        }
                        
                    } catch {
                        S_Log._E_(description: C_Aws_S3_Storage_Module.ACTIVITY_NAME + " :: getS3FileUpload :: Exception", data: [ "\(error)" ] )
                        
                        // ---------------------------------------------
                        // MARK: PermanentRedirect: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.
                        // ---------------------------------------------
                        // MARK: AWSClient 초기화 액세스 키 정보가 올바르지 않거나, region 에 버킷이 없는 경우 에러 발생
                        // ---------------------------------------------
                        
                        completion(false) // [Return CallBack]
                        
                    }
                    
                    // ---------------------------------------------
                    // MARK: AWSClient 리소스 정리 반드시 필요 (SotoCore/AWSClient.swift:105: Assertion failed: AWSClient not shut down before the deinit. Please call client.syncShutdown() when no longer needed.)
                    // ---------------------------------------------
                    try await client.shutdown()
                }
                else {
                    S_Log._E_(description: C_Aws_S3_Storage_Module.ACTIVITY_NAME + " :: getS3FileUpload :: Error", data: [ "Input Data Is Null" ] )
                    
                    completion(false) // [Return CallBack]
                    
                }
                
            }
        }
    }
    
    
} // [클래스 종료]

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





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

[Soto] SPM 사용해 Soto 라이브러리 ( AWS 기능 사용 ) 설치 방법 정리

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


[간단 설명] SPM (Swift Package Manager) 사용해 특정 라이브러리 버전 변경 및 업데이트 방법

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


[Soto Git 다운로드 주소]

https://github.com/soto-project/soto.git


[Soto 라이브러리 release 버전 참고]

https://github.com/soto-project/soto/releases?page=1

// --------------------------------------------------------------------------------------
 
728x90
반응형
Comments