Exemplo n.º 1
0
def upload_assets(user, secret, bucket, _retries=5):
    access_key = user if user else os.environ['AWS_ACCESS_KEY_ID']
    access_secret = secret if secret else os.environ['AWS_SECRET_ACCESS_KEY']
    bucket = bucket if bucket else os.environ['S3_BUCKET_NAME']

    retries = 0

    import subprocess
    # build assets and capture the output
    print 'Building assets...'
    proc = subprocess.Popen(['python', 'manage.py', 'assets', 'build'],
                            stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT)
    proc.wait()

    print 'Connecting to S3...'
    conn, bucket = connect_to_s3(access_key, access_secret, bucket)
    print 'Uploading files...'

    for path in proc.communicate()[0].split('\n')[:-1]:
        key = path.split('public')[1]
        print 'Uploading {}'.format(key)
        while retries <= _retries:
            try:
                upload_file(key,
                            bucket,
                            root=current_app.config['APP_DIR'] + '/static')
                break
            except Exception, e:
                print 'Error: {}'.format(e), 'Retrying...'
                retries += 1
        if retries > _retries:
            print 'File {} did not upload'.format(key)
        retries = 0
Exemplo n.º 2
0
def upload_assets(user, secret, bucket, _retries=5):
    access_key = user if user else os.environ['AWS_ACCESS_KEY_ID']
    access_secret = secret if secret else os.environ['AWS_SECRET_ACCESS_KEY']
    bucket = bucket if bucket else os.environ['S3_BUCKET_NAME']

    retries = 0

    import subprocess
    # build assets and capture the output
    print 'Building assets...'
    proc = subprocess.Popen(
        ['python', 'manage.py', 'assets', 'build'],
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT
    )
    proc.wait()

    print 'Connecting to S3...'
    conn, bucket = connect_to_s3(access_key, access_secret, bucket)
    print 'Uploading files...'

    for path in proc.communicate()[0].split('\n')[:-1]:
        key = path.split('public')[1]
        print 'Uploading {}'.format(key)
        while retries <= _retries:
            try:
                upload_file(key, bucket, root=current_app.config['APP_DIR'] + '/static')
                break
            except Exception, e:
                print 'Error: {}'.format(e), 'Retrying...'
                retries += 1
        if retries > _retries:
            print 'File {} did not upload'.format(key)
        retries = 0
Exemplo n.º 3
0
def upload_assets(user, secret, bucket, _retries=5):
    access_key = user if user else os.environ["AWS_ACCESS_KEY_ID"]
    access_secret = secret if secret else os.environ["AWS_SECRET_ACCESS_KEY"]
    bucket = bucket if bucket else os.environ["S3_BUCKET_NAME"]

    retries = 0

    import subprocess

    # build assets and capture the output
    print "Building assets..."
    proc = subprocess.Popen(
        ["python", "manage.py", "assets", "build"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT
    )
    proc.wait()

    print "Connecting to S3..."
    conn, bucket = connect_to_s3(access_key, access_secret, bucket)
    print "Uploading files..."

    for path in proc.communicate()[0].split("\n")[:-1]:
        key = path.split("public")[1]
        print "Uploading {}".format(key)
        while retries <= _retries:
            try:
                upload_file(key, bucket, root=current_app.config["APP_DIR"] + "/static")
                break
            except Exception, e:
                print "Error: {}".format(e), "Retrying..."
                retries += 1
        if retries > _retries:
            print "File {} did not upload".format(key)
        retries = 0
Exemplo n.º 4
0
    def upload_document(self, _id):
        '''Take the document and filename and either upload it to S3 or the local uploads folder

        Arguments:
            _id: The id of the :py:class:`~purchasing.models.front.Opportunity`
                the document will be attached to

        Returns:
            A two-tuple of (the document name, the document filepath/url)
        '''
        if self.document.data is None or self.document.data == '':
            return None, None

        filename = secure_filename(self.document.data.filename)

        if filename == '':
            return None, None

        _filename = 'opportunity-{}-{}'.format(_id, filename)

        if current_app.config.get('UPLOAD_S3') is True:
            # upload file to s3
            conn, bucket = connect_to_s3(
                current_app.config['AWS_ACCESS_KEY_ID'],
                current_app.config['AWS_SECRET_ACCESS_KEY'],
                current_app.config['UPLOAD_DESTINATION']
            )
            _document = bucket.new_key(_filename)
            aggressive_headers = _get_aggressive_cache_headers(_document)
            _document.set_contents_from_file(self.document.data, headers=aggressive_headers, replace=True)
            _document.set_acl('public-read')
            return _document.name, _document.generate_url(expires_in=0, query_auth=False)

        else:
            try:
                os.mkdir(current_app.config['UPLOAD_DESTINATION'])
            except:
                pass

            filepath = os.path.join(current_app.config['UPLOAD_DESTINATION'], _filename)
            self.document.data.save(filepath)
            return _filename, filepath
Exemplo n.º 5
0
    def upload_document(self, _id):
        '''Take the document and filename and either upload it to S3 or the local uploads folder

        Arguments:
            _id: The id of the :py:class:`~purchasing.models.front.Opportunity`
                the document will be attached to

        Returns:
            A two-tuple of (the document name, the document filepath/url)
        '''
        if self.document.data is None or self.document.data == '':
            return None, None

        filename = secure_filename(self.document.data.filename)

        if filename == '':
            return None, None

        _filename = 'opportunity-{}-{}'.format(_id, filename)

        if current_app.config.get('UPLOAD_S3') is True:
            # upload file to s3
            conn, bucket = connect_to_s3(
                current_app.config['AWS_ACCESS_KEY_ID'],
                current_app.config['AWS_SECRET_ACCESS_KEY'],
                current_app.config['UPLOAD_DESTINATION']
            )
            _document = bucket.new_key(_filename)
            aggressive_headers = _get_aggressive_cache_headers(_document)
            _document.set_contents_from_file(self.document.data, headers=aggressive_headers, replace=True)
            _document.set_acl('public-read')
            return _document.name, _document.generate_url(expires_in=0, query_auth=False)

        else:
            try:
                os.mkdir(current_app.config['UPLOAD_DESTINATION'])
            except:
                pass

            filepath = os.path.join(current_app.config['UPLOAD_DESTINATION'], _filename)
            self.document.data.save(filepath)
            return _filename, filepath