카메라 촬영을 통한 결과를 FileManager의 document directory에 저장하고, 그 경로를 URL 형식으로 변환하여 S3를 업로드하는 내용입니다.
코코아팟 설치를 해줍니다.
# AWS
pod 'AWSS3', '~> 2.27.1'
poolId를 발급받아 AWSCognitoCredentialsProvider과 AWSServiceConfiguration을 생성 후, Default Service Configuration을 이니셜라이징 해줍니다.
func configure() {
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .APNortheast2, identityPoolId: "INSERT-POOL-ID-HERE")
guard let configuration = AWSServiceConfiguration(region: .APNortheast2, credentialsProvider: credentialsProvider) else { return }
AWSServiceManager.default().defaultServiceConfiguration = configuration
}
버킷네임과 키값으로 넣을 path 그리고 기타 contentType을 설정하여 파일을 업로드합니다.
let bucketName = "BUCKET_NAME"
var completionHandler: AWSS3TransferUtilityUploadCompletionHandlerBlock?
func upload() {
let fileUrl = URL(fileURLWithPath: "PATH")
let expression = AWSS3TransferUtilityUploadExpression()
expression.progressBlock = { (task: AWSS3TransferUtilityTask,progress: Progress) -> Void in
print(progress.fractionCompleted) //2
if progress.isFinished{ //3
print("Upload Finished...")
//do any task here.
}
}
expression.setValue("public-read-write", forRequestHeader: "x-amz-acl")
completionHandler = { task, error -> Void in
if let _ = error {
print("Failure uploading file")
} else {
print("Success uploading file")
}
}
AWSS3TransferUtility.default().uploadFile(fileUrl, bucket: bucketName, key: "Path", contentType: "image/png", expression: expression, completionHandler: completionHandler).continueWith { task -> AnyObject? in
if let _ = task.error {
print("Error uploading file: \(String(describing: task.error?.localizedDescription))")
}
if let _ = task.result {
print("start upload...")
}
return nil
}
}
그런데 업로드를 하는 도중, 이런 에러가 나옵니다.
com.amazonaws.AWSCognitoIdentityErrorDomain error 10
처음 이니셜라이징할때 리전타입을 잘못 입력했었습니다.
func configure() {
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .APNortheast2, identityPoolId: "INSERT-POOL-ID-HERE")
guard let configuration = AWSServiceConfiguration(region: .APNortheast2, credentialsProvider: credentialsProvider) else { return }
AWSServiceManager.default().defaultServiceConfiguration = configuration
}
Info.plist를 소스 코드 보기로 변경하여 AWS 관련 설정들을 입력해줍니다.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>amazonaws.com</key>
<dict>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
<key>amazonaws.com.cn</key>
<dict>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
<key>AWS</key>
<dict>
<key>CredentialsProvider</key>
<dict>
<key>CognitoIdentity</key>
<dict>
<key>Default</key>
<dict>
<key>PoolId</key>
<string>---INSERT POOL ID---</string>
<key>Region</key>
<string>---INSERT REGION---</string>
</dict>
</dict>
</dict>
<key>S3TransferManager</key>
<dict>
<key>Default</key>
<dict>
<key>Region</key>
<string>---INSERT REGION---<</string>
</dict>
</dict>
</dict>
사진을 촬영 후, 다시 s3에 업로드해봅니다.
아래와 같이 이미지 파일이 업로드된 것을 확인할 수 있습니다.
참고
https://medium.com/@iamayushverma/uploading-photos-videos-files-to-aws-s3-using-swift-4-1241f690a993
반응형
'iOS' 카테고리의 다른 글
Fastlane을 활용한 프로젝트 배포 자동화 구축 (0) | 2022.05.06 |
---|---|
MVC to MVVM (0) | 2022.05.06 |
Apple Enterprise 와 Apple Business Manager (0) | 2021.10.20 |
iOS Framework 만들어보기 (0) | 2021.07.10 |
스위프트 패키지 매니저(Swift Package Manager) 라이브러리 만들기 (0) | 2021.07.09 |