Giter Site home page Giter Site logo

maximbilan / swift-amazon-s3-uploading-tutorial Goto Github PK

View Code? Open in Web Editor NEW
76.0 3.0 13.0 717 KB

How to upload a file to Amazon S3 using Swift

License: MIT License

Swift 97.61% Ruby 2.39%
amazon amazon-s3-tutorial amazon-s3-storage upload upload-file tutorial ios swift

swift-amazon-s3-uploading-tutorial's Introduction

How to upload a file to Amazon S3 using Swift

I would like to share a simple tutorial how to upload a to Amazon S3 in iOS using Swift. Let’s go.

alt tag

We need to add Amazon S3 framework to your project.
In this example I will do this with helping Cocoapods.

Create a Podfile:

platform :ios, '8.0'
inhibit_all_warnings!
use_frameworks!
target 'AmazonS3Upload' do
pod 'AWSS3'
end

Run the next command from Terminal:

pod install

Open the generated workspace. And after that we can implement uploading of files using frameworks from Pods.

We need to import 2 modules:

import AWSS3
import AWSCore

Set up a AWS configuration using your credentials. For example:

let accessKey = "..."
let secretKey = "..."
let credentialsProvider = AWSStaticCredentialsProvider(accessKey: accessKey, secretKey: secretKey)
let configuration = AWSServiceConfiguration(region: AWSRegionType.usEast1, credentialsProvider: credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration

Create an upload request:

let url = ...URL to your file...
let remoteName = "Name of uploaded file"
let S3BucketName = "Name of your bucket on Amazon S3"
let uploadRequest = AWSS3TransferManagerUploadRequest()!
uploadRequest.body = url
uploadRequest.key = remoteName
uploadRequest.bucket = S3BucketName
uploadRequest.contentType = "image/jpeg"
uploadRequest.acl = .publicRead

And upload using AWSS3TransferManager.

let transferManager = AWSS3TransferManager.default()
transferManager?.upload(uploadRequest).continue({ (task: AWSTask) -> Any? in
  if let error = task.error {
    print("Upload failed with error: (\(error.localizedDescription))")
  }
  if let exception = task.exception {
    print("Upload failed with exception (\(exception))")
  }
  if task.result != nil {
    let url = AWSS3.default().configuration.endpoint.url
    let publicURL = url?.appendingPathComponent(uploadRequest.bucket!).appendingPathComponent(uploadRequest.key!)
    print("Uploaded to:\(publicURL)")
  }
  return nil
})

swift-amazon-s3-uploading-tutorial's People

Contributors

maximbilan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

swift-amazon-s3-uploading-tutorial's Issues

Getting No such module 'AWSS3'

I have installed through pods as you suggested in documentation file.
Its installed successfully, but, after imported the framework into class, its throwing as "No such module 'AWSS3'", even I removed derived data.

Can you help me, how to solve this?

multiple region support in same application

In my iOS app, we have to download and upload files on different regions but access key and secret key are same for both region.

when I upload file on s3 bucket on one region its work fine but when I try to upload on another region by changing AWSServiceConfiguration its not uploading/download file from another region, and when I debug then I get wrong region set which Is fist time set region.

I am using pod 'AWSS3', '~> 2.9'
how can I fix this?

thanks you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.