Giter Site home page Giter Site logo

sbolel / ionic-image-upload Goto Github PK

View Code? Open in Web Editor NEW
26.0 3.0 6.0 5.71 MB

Ionic Plugin for Uploading Images to Amazon S3

License: MIT License

JavaScript 2.39% CSS 97.03% HTML 0.59%
ionic-plugin ionic ionic-framework image-uploader aws aws-s3 s3 s3-bucket s3-storage image

ionic-image-upload's Introduction

Ionic Image Upload

circle-ci js-standard

An Ionic Starter App that demonstrates using the image-uploader open source plugin for uploading image files to an S3 bucket.

Getting Started

Install image-uploader via Bower:

bower install --save s3-image-uploader

Include image-upload.bundle.min.js in your app:

<script src="lib/s3-image-uploader/dist/image-uploader.min.js"></script>

Or, with the Amazon SDK included:

<script src="lib/s3-image-uploader/dist/image-uploader.bundle.min.js"></script>

Upload an image from a controller

.controller('UploadController', function ($scope){
  var imageUploader = new ImageUploader()
  $scope.file = {}
  $scope.upload = function() {
    imageUploader.push($scope.file, function(data){
      console.log('File uploaded Successfully', $scope.file, data)
      $scope.uploadUri = data.url
      $scope.$digest()
    })
  }
})

Full Example

File upload form HTML:

<ion-content ng-controller="UploadController">
  <input class="bottom-marg-15" type="file" name="file" file="file">
  <button class="button button-small button-full button-positive" ng-disabled="file.size&amp&ampfalse || uploadProgress&gt0" ng-click="upload()" ng-cloak="">Upload</button>
  <img class="fit" ng-if="uploadUri" ng-src="{{uploadUri}}">
  <a ng-href="{{uploadUri}}" ng-bind="uploadUri"></a>
</ion-content>

file Directive:

.directive('file', function() {
  return {
    restrict: 'AE',
    scope: {
      file: '@'
    },
    link: function(scope, el, attrs){
      el.bind('change', function(event){
        var files = event.target.files
        var file = files[0]
        if(file.size>0){
          scope.file = file
          scope.$parent.file = file
        } else {
          scope.file = {}
          scope.$parent.file = {} 
        }
        scope.$apply()
      })
    }
  }
})

Configuring AWS S3

You are welcome to use the public S3 bucket that is preconfigured in src/image-upload.js. This public bucket will only accept files with JPG and PNG extensions up to 10MB in size. The files are automatically deleted after 24 hours.

To use your own Amazon S3 bucket, change the information in src/image-upload.js and uglify by running grunt in Terminal from the project directory.

Setting up an AWS S3 Bucket for use with Ionic Image Upload

Below is a summary of Uploading To S3 With AngularJS by Cheyne Wallace

To setup an S3 bucket for use with the Ionic Image Upload plugin, we need to:

  • Configure an AWS S3 bucket by creating a "public" IAM account:
    • The IAM user will only have permission to PUT files into a particular AWS Bucket and nothing else.
    • This users API key will be public -- anyone will be able to upload to your bucket if they use this key.
  • Configure the bucket to expire all objects within 24 hours.
    • Even if someone uploads a 10 Gigabyte file, it will eventually be deleted.
  • Configure CORS to prevent uploading of content from anywhere other than your own domain.
  • Create a server to transfer uploaded files from the temporary bucket to a permanent bucket:
    • When a new file is uploaded to this temporary bucket from the app
    • App will send the details of the file to the server
    • Server will perform any necessary transformations, encryption, resizing, or processing, and,
    • Server will move the file into a permanent bucket.

1. Create the IAM User

  1. Open AWS console to the "Security Credentials" section.
  2. Create a new user and call it something like "app_public".
  3. Make sure you download the key information when it is presented, this is what we’ll be feeding into our app later to upload with.
  4. Under the permissions section, click "attach a new policy", then select the policy generator.
  5. Select Amazon S3 as the service and only select the PutObject action from the drop down list.
  6. The ARN is an Amazon Resource Name. This will look like arn:aws:s3:::your_bucket_name
  7. Click "add statement", then save and apply policy.

Now your user has write-only access to the bucket.

Your policy is going to look something like this:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Stmt126637111000",
      "Effect": "Allow",
      "Action": [
        "s3:PutObject"
      ],
      "Resource": [
        "arn:aws:s3:::your_bucket_name"
      ]
    }
  ]
}

2. Configure CORS And Expiry On The Bucket

Go to the AWS S3 console, click your bucket, then click the Properties button. You'll use the "Add CORS Configuration" button to configure your bucket to only allow PUT requests from particular origins.

You can use the following sample config -- edit to reflect your development, production and staging environments.

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>http://localhost:3000</AllowedOrigin>
        <AllowedOrigin>https://www.yourdomain.com</AllowedOrigin>
        <AllowedOrigin>http://staging.yourdomain.com</AllowedOrigin>
        <AllowedMethod>PUT</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <ExposeHeader>x-amz-server-side-encryption</ExposeHeader>
        <ExposeHeader>x-amz-request-id</ExposeHeader>
        <ExposeHeader>x-amz-id-2</ExposeHeader>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

Expire the objects in this bucket after some short period to prevent malicious use. Your server side code should handle moving and deleting valid files so you can assume those that are left after 24 hours are not meant to be there.

  1. From your S3 console, view a bucket, and then click Properties.
  2. Expand the "Lifecycle Rules" section and follow the prompts.
  3. Set the action to "Permanently Delete Only" and set it for 1 day -- this will delete any objects in the bucket that are older than 1 day permanently.

Now, you're ready to use this bucket in your Ionic Image Upload app!

ionic-image-upload's People

Contributors

sbolel 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

Watchers

 avatar  avatar  avatar

ionic-image-upload's Issues

Not working on android 4.4.2

Working fine on chrome using ionic serve, but not working on device. Returns this error on debugging in app console.
Please help fix.

TypeError: Object function Object() { [native code] } has no method 'assign'
    at Object.b (file:///android_asset/www/lib/s3-image-uploader/dist/image-uploader.bundle.min.js:38:25746)
    at new <anonymous> (file:///android_asset/www/js/app.js:428:23)
    at Object.invoke (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:17995:19)
    at $controllerInit (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:23397:34)
    at nodeLinkFn (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:22335:34)
    at compositeLinkFn (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:21703:13)
    at compositeLinkFn (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:21706:13)
    at nodeLinkFn (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:22387:24)
    at compositeLinkFn (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:21703:13)
    at nodeLinkFn (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:22387:24) ionic.bundle.js:26794
(anonymous function) ionic.bundle.js:26794
(anonymous function) ionic.bundle.js:23507
Scope.$broadcast ionic.bundle.js:30720
$state.transition.resolved.then.$state.transition ionic.bundle.js:52157
processQueue ionic.bundle.js:29127
(anonymous function) ionic.bundle.js:29143
Scope.$eval ionic.bundle.js:30395
Scope.$digest ionic.bundle.js:30211
Scope.$apply ionic.bundle.js:30503
(anonymous function) ionic.bundle.js:65426
defaultHandlerWrapper ionic.bundle.js:16787
eventHandler ionic.bundle.js:16775
triggerMouseEvent ionic.bundle.js:2953
tapClick ionic.bundle.js:2942
tapTouchEnd ionic.bundle.js:3069

Cannot read property 'config' of undefined

ionic.bundle.js:26794 TypeError: Cannot read property 'config' of undefined
    at Object.b (http://localhost:8100/lib/s3-image-uploader/dist/image-uploader.min.js:2:77)
    at new <anonymous> (http://localhost:8100/js/dcontrollers.js:299:23)
    at Object.instantiate (http://localhost:8100/lib/ionic/js/ionic.bundle.js:18010:14)
    at $controller (http://localhost:8100/lib/ionic/js/ionic.bundle.js:23412:28)
    at self.appendViewElement (http://localhost:8100/lib/ionic/js/ionic.bundle.js:59900:24)
    at Object.render (http://localhost:8100/lib/ionic/js/ionic.bundle.js:57893:41)
    at Object.init (http://localhost:8100/lib/ionic/js/ionic.bundle.js:57813:20)
    at self.render (http://localhost:8100/lib/ionic/js/ionic.bundle.js:59759:14)
    at self.register (http://localhost:8100/lib/ionic/js/ionic.bundle.js:59717:10)
    at updateView (http://localhost:8100/lib/ionic/js/ionic.bundle.js:65398:23) <ion-nav-view name="dgroups" class="view-container tab-content" nav-view="active" nav-view-transition="ios">(anonymous function) @ ionic.bundle.js:26794(anonymous function) @ ionic.bundle.js:23507invokeLinkFn @ ionic.bundle.js:22995nodeLinkFn @ ionic.bundle.js:22392compositeLinkFn @ ionic.bundle.js:21703publicLinkFn @ ionic.bundle.js:21583tabSelected @ ionic.bundle.js:67026$digest @ ionic.bundle.js:30239$apply @ ionic.bundle.js:30503done @ ionic.bundle.js:24824completeRequest @ ionic.bundle.js:25022requestLoaded @ ionic.bundle.js:24963

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.