Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿ“ฎ ObjectStorage์— Image ์ €์žฅ API ์—ฐ๋™ #32

Merged
merged 3 commits into from
Nov 18, 2023

Conversation

heejinnn
Copy link
Collaborator

@heejinnn heejinnn commented Nov 17, 2023

์ž‘์—… ์ด์œ 

์™œ ObjectStorage ์‚ฌ์šฉ?

โ—๏ธDB์— ์ €์žฅํ•˜๋Š” ๊ฒƒ๊ณผ ์ฐจ์ด์ 


  1. ๋ฐ์ดํ„ฐ ์œ ํ˜•:

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค: ์ฃผ๋กœ ๊ตฌ์กฐํ™”๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ, ํ…Œ์ด๋ธ”๊ณผ ์—ด์˜ ํ˜•ํƒœ๋กœ ์ €์žฅ
Object Storage: ๋น„์ •ํ˜• ๋ฐ์ดํ„ฐ (์ด๋ฏธ์ง€, ๋™์˜์ƒ, ๋ฌธ์„œ ๋“ฑ)๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐ ํšจ๊ณผ์ , ํŒŒ์ผ ์ž์ฒด์™€ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ํ•จ๊ป˜ ์ €์žฅ

  1. ์šฉ๋Ÿ‰ ๋ฐ ํ™•์žฅ์„ฑ:

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค: ์ฃผ๋กœ ์ •ํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋ฏ€๋กœ, ์Šคํ‚ค๋งˆ ๋ฐ ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค. ํ™•์žฅ์„ฑ์ด ์žˆ์ง€๋งŒ ์ผ์ • ๊ทœ๋ชจ ์ด์ƒ์˜ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํ™•์žฅ์ด ๋ณต์žกํ•  ์ˆ˜ ์žˆ๋‹ค.
Object Storage: ๋Œ€์šฉ๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ถ„์‚ฐ ์•„ํ‚คํ…์ฒ˜๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์‰ฝ๊ฒŒ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. ์ ‘๊ทผ์„ฑ:

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค: ๊ตฌ์กฐํ™”๋œ ์ฟผ๋ฆฌ ์–ธ์–ด(SQL)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผ
Object Storage: ์ฃผ๋กœ RESTful API๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•˜๋ฉฐ, ๊ฐ„๋‹จํ•œ HTTP ์š”์ฒญ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. ๋น„์šฉ ๊ตฌ์กฐ:

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค: ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ ์–‘์— ๋”ฐ๋ฅธ ๋น„์šฉ์ด ์กด์žฌํ•˜๋ฉฐ, ์ฟผ๋ฆฌ ์ˆ˜ํ–‰์— ๋”ฐ๋ผ ๋น„์šฉ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.
Object Storage: ์ฃผ๋กœ ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ์˜ ์–‘๊ณผ ์ „์†ก๋œ ๋ฐ์ดํ„ฐ ์–‘์— ๋”ฐ๋ผ ๋น„์šฉ์ด ๋ฐœ์ƒํ•œ๋‹ค. ์‚ฌ์šฉํ•œ ์šฉ๋Ÿ‰๋งŒํผ๋งŒ ๋น„์šฉ์„ ์ง€๋ถˆํ•˜๋ฏ€๋กœ ์˜ˆ์ธก ๊ฐ€๋Šฅ

์ •๋ฆฌํ•˜์ž๋ฉด, ObjectStorage๋Š” RESTful API๋ฅผ ํ†ตํ•ด ํŽธ๋ฆฌํ•œ ๋ฐ์ดํ„ฐ ์ ‘๊ทผ๊ณผ ํฐ ๋ฐ์ดํ„ฐ๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ , ๊ทธ๋ฆฌ๊ณ  ๋น„์ •ํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐ ํšจ๊ณผ์ ์ด๋ฏ€๋กœ ObjectStorage์— Image๋ฅผ ์ €์žฅํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค.

์ž‘์—… ์‚ฌํ•ญ


  1. presignedUrl ๋ฐœ๊ธ‰

presignedUrl์— POST ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด payload ๊ฐ’์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
payload๋Š” {api}/{dirname}/uuid.{extension}?{queryitem} ์™€ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ dirname๊ณผ extension ๊ทธ๋ฆฌ๊ณ  ?๋’ค์— queryItem๋“ค์„ ๋ฐ›์•„์˜จ๋‹ค.

๋ฐ›์•„์˜จ payload๋ฅผ ๋ฐ”๋กœ ์‚ฌ์šฉํ•˜์ง€์•Š๊ณ  queryitem๊ณผ ๋ถ„๋ฆฌํ•˜์—ฌ ์ €์žฅํ•œ๋‹ค.
?์•ž๊นŒ์ง€ url์„ PAYLOADURL.PAYLOAD์— ์ €์žฅํ•˜๊ณ  ๋‚˜๋จธ์ง€ queryitem๋“ค์„ PAYLOADURL์˜ ์š”์†Œ๋“ค์— ์ €์žฅํ•ด์ค€๋‹ค.

 //presignedURL ๋ฐœ๊ธ‰ ๋ฐ›๊ธฐ
AlamofireManager.shared.presignedURL("profile", "jpeg") { result in
    switch result {
    case .success(let data):
        // Handle success
        if let responseData = data {
            do {
                let jsonObject = try JSONSerialization.jsonObject(with: responseData, options: []) as? [String: Any]

                if let payloadValue = jsonObject?["payload"] as? String,
                   let payloadURL = URL(string: payloadValue),
                   let components = URLComponents(url: payloadURL, resolvingAgainstBaseURL: false),
                   let queryItems = components.queryItems {

                    //'?'์ „๊นŒ์ง€ payload ์„ค์ •
                    if let range = payloadValue.range(of: "?") {
                        PAYLOADURL.PAYLOAD = String(payloadValue[..<range.lowerBound])
                    } else {
                        PAYLOADURL.PAYLOAD = payloadValue
                    }

                    // query item์„ PATLOADURL์— ์ €์žฅ
                    PAYLOADURL.algorithm = queryItems[0].value ?? ""
                    PAYLOADURL.credential = queryItems[1].value ?? ""
                    PAYLOADURL.date = queryItems[2].value ?? ""
                    PAYLOADURL.expires = queryItems[3].value ?? ""
                    PAYLOADURL.signature = queryItems[4].value ?? ""
                    PAYLOADURL.signedHeaders = queryItems[5].value ?? ""
                    PAYLOADURL.acl = queryItems[6].value ?? ""

                    print("JSON Object: \(jsonObject ?? [:])")
                    print(PAYLOADURL.PAYLOAD)
                } else {
                    print("Payload key not found in the JSON response.")
                }
            } catch {
                print("Error parsing JSON: \(error)")
            }
        }
    case .failure(let error):
        // Handle failure
        print("Error: \(error)")
    }
}

์•„๋ž˜์˜ ๋‘ ์ฟผ๋ฆฌ๋ฌธ๋„ api ๋„ฃ์–ด์„œ ๋ณด๋‚ด์ฃผ๊ณ  dirname๊ณผ extension ๊ฐ’์€ body์— ๋„ฃ์–ด์„œ POST ์š”์ฒญ์„ ๋ณด๋‚ธ๋‹ค.

URLQueryItem(name: "result", value: "true"),
URLQueryItem(name: "blocking", value: "true")

  1. ObjectStorage Image ์ €์žฅ

PAYLOADURL์— ์ €์žฅ๋œ queryItem๋“ค์„ ๊ฐ€์ ธ์™€ put์š”์ฒญ์„ ๋ณด๋‚ด๋Š” url์— query๋ฌธ์„ ์‚ฝ์ž…ํ•ด์ค€๋‹ค.
query๋ฌธ์„ ์ด๋Ÿฐ ์‹์œผ๋กœ ๋”ฐ๋กœ ์ €์žฅํ•ด์„œ queryitem์œผ๋กœ ์ถ”๊ฐ€ํ•ด์„œ ๋ณด๋‚ด์ง€ ์•Š์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋‚œ๋‹ค....

//objectStorage Image ์ €์žฅ
            AlamofireManager.shared.uploadImage(pickedImage) { result in
               switch result {
               case .success(let data):
                   // Handle success
                   if let unwrappedData = data,
                      let resultString = String(data: unwrappedData, encoding: .utf8) {
                       print("Success: \(resultString)")
                   } else {
                       print("Success with nil or non-text data")
                   }

               case .failure(let error):
                   // Handle failure
                   print("Error: \(error)")
               }
           }

๋‹ค์Œ๊ณผ ๊ฐ™์ด queryitem๋“ค์„ url์— ์‚ฝ์ž…ํ•ด์คฌ๋‹ค.


 case .uploadImage(let image):
            request = createURLRequestForBody(url: baseURL, image: image)
            // Add X-Amz-Algorithm query parameter
            var components = URLComponents(url: baseURL, resolvingAgainstBaseURL: false)
            components?.queryItems =
                [URLQueryItem(name: "X-Amz-Algorithm", value: PAYLOADURL.algorithm),
                 URLQueryItem(name: "X-Amz-Credential", value: PAYLOADURL.credential),
                 URLQueryItem(name: "X-Amz-Date", value: PAYLOADURL.date),
                 URLQueryItem(name: "X-Amz-Expires", value: PAYLOADURL.expires),
                 URLQueryItem(name: "X-Amz-Signature", value: PAYLOADURL.signature),
                 URLQueryItem(name: "X-Amz-SignedHeaders", value: PAYLOADURL.signedHeaders),
                 URLQueryItem(name: "x-amz-acl", value: PAYLOADURL.acl)
                ]

            if let urlWithQuery = components?.url {
                request.url = urlWithQuery
                
                print("requestURL: \(request.url)")
            }

image๋Š” body์— ๋„ฃ์–ด query ํŒŒ๋ผ๋ฏธํ„ฐ ์‚ฝ์ž…ํ•œ url์— PUT ์š”์ฒญ์„ ๋ณด๋ƒˆ๋‹ค.

์ด์Šˆ ์—ฐ๊ฒฐ

close #31

@heejinnn heejinnn self-assigned this Nov 17, 2023
@psychology50 psychology50 self-requested a review November 18, 2023 02:16
@psychology50 psychology50 added the enhancement New feature or request label Nov 18, 2023
Copy link
Member

@psychology50 psychology50 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ „์— iOS ๋กœ๊ทธ ์•Œ๋ ค๋“œ๋ฆฐ ๊ฑฐ ์ž˜ ์“ฐ๊ณ  ๊ณ„์‹œ๋„ค์š”. ๋‹ค๋งŒ ๋””๋ฒ„๊น… ์šฉ์œผ๋กœ print๋ฅผ ์“ฐ์…”์„œ ํ˜ผ์šฉ ์ค‘์ธ ์ƒํƒœ์ธ ๊ฑฐ ๊ฐ™์€๋ฐ, ์ด๊ฒƒ๋„ ์ง€์šฐ์‹œ๊ฑฐ๋‚˜ log๋กœ ๋ฐ”๊พธ๋Š” ๊ฒŒ ์ข‹์„ ๋“ฏ ํ•ฉ๋‹ˆ๋‹ค.
์ง€๊ธˆ์€ Alamofire์˜ Session.default๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๊ณ„์‹œ์ง€๋งŒ, ๋‚˜์ค‘์—๋Š” ์ปค์Šคํ…€ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒŒ ํŽธํ•ด์ง€๋Š” ์‹œ์ ์ด ์˜ฌ ๊ฑฐ์˜ˆ์š”. ๊ทธ๋•Œ๋ผ๋„ Interceptor๋ฅผ ์ ์šฉํ•ด๋ณด์‹œ๋Š” ๊ฑธ ๊ถŒํ•ด๋ด…๋‹ˆ๋‹ค. ์ €๋„ ๋„์™€๋“œ๋ฆด๊ฒŒ์š”.
์ž‘์—…ํ•˜์‹œ๋Š๋ผ ์ˆ˜๊ณ  ๋งŽ์œผ์…จ์Šต๋‹ˆ๋‹ค.

static var acl = ""
}

enum QueryParameter: String {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

url์„ ๋ฐ›์•„์„œ ์ฟผ๋ฆฌ๋กœ ๋ถ„๋ฆฌํ•ด์•ผ ํ•  ์ผ์ด ๋งŽ์„์ง€๋Š” ์ž˜ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ, ๋งŒ์•ฝ ๊ทธ๋Ÿด ์ผ์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค๋ฉด QueryParameter๋ณด๋‹ค๋Š” ObjStorageQueryParam..? ๊ฐ™์€ ๊ฒŒ ๋” ๋‚˜์„ ๊ฑฐ ๊ฐ™์•„์š”.


import Foundation

enum API{
static let BASE_URL : String = "http://www.fitapet.co.kr:8080/api/v1/"
static let PRESIGNEDURL : String = "https://h1d5jjb17z.apigw.ntruss.com/obj/photo/"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

presigned url๋„ ์ด๋ฒˆ์—๋Š” /photo ํ•˜์œ„ ๋””๋ ‰ํ† ๋ฆฌ์— ๋Œ€ํ•œ ๋‚ด์šฉ์ด์ง€๋งŒ, ์ €ํฐ profile์ด๋‚˜ pet-profile๋„ ์žˆ๋‹ค๋Š” ์ ์„ ์ƒ๊ฐํ•ด์„œ ๋ฌธ์ž์—ด์„ /obj๊นŒ์ง€๋งŒ ์ง€์ •ํ•˜๊ฑฐ๋‚˜, ๋‹ค๋ฅธ ๋ณ€์ˆ˜๋ช…์„ ์„ ํƒํ•˜๋Š” ๊ฒŒ ์ข‹์„ ๋“ฏ ํ•ฉ๋‹ˆ๋‹ค.

@psychology50 psychology50 merged commit 6300748 into develop Nov 18, 2023
2 checks passed
@heejinnn heejinnn changed the title ObjectStorage์— Image ์ €์žฅ ๐Ÿ“ฎ ObjectStorage์— Image ์ €์žฅ API ์—ฐ๋™ Jan 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ObjectStorage Image ์ €์žฅ
2 participants