/*
初始化 Multipart Upload
使用 Multipart Upload 模式传输数据前,必须先调用该接口来通知 OSS 初始 化一个 Multipart Upload 事件

初始化结束后，触发ready事件
@param {Object} Bucket && Key
330479
function createMultipartUpload(details) {
    cachedClient.createMultipartUpload(details, function (err, data) {
      if (err) return abortUpload(err);
      multipartUploadID = data.UploadId;
      multipartUploadResult = data;
      e.emit('ready');
    });
  }
53741
function uploadTemplate(bucketName, filename, path) {
    return spinner(
        new Promise((resolve, reject) =>
            fs.stat(path, (error, stats) => {
                if (!stats) {
                    reject(new Error(`File ${chalk.bold(path)} not found`));
                }
                const upload = s3.upload(
                    {
                        Bucket: bucketName,
                        Key: filename,
                        Body: fs.createReadStream(path)
                    },
                    {
                        partSize: Math.max(stats.size, S3_MIN_PART_SIZE),
                        queueSize: 1
                    }
                );
                let bar;
                upload.on('httpUploadProgress', evt => {
                    if (!bar && evt.total) {
                        const total = evt.total / 1000000;
                        bar = new ProgressBar('uploading [:bar] :percent :etas', {
                            complete: '=',
                            incomplete: ' ',
                            width: 20,
                            total,
                            clear: true
                        });
                    }

                    const curr = evt.loaded / 1000000;
                    bar.tick(curr - bar.curr);
                });
                return upload
                    .promise()
                    .then(resolve)
                    .catch(reject);
            })
        )
    );
}
288237
public Future<AsperaTransaction> upload(String bucket, File localFileName, String remoteFileName) {
		
		return upload(bucket, localFileName, remoteFileName, asperaConfig, null);
	}
1075064
function( key, blob, filename ) {
                var me = this,
                    opts = me.options;
    
                if ( me.getRuid() ) {
                    me.disconnectRuntime();
                }
    
                // 连接到blob归属的同一个runtime.
                me.connectRuntime( blob.ruid, function() {
                    me.exec('init');
                });
    
                me._blob = blob;
                opts.fileVar = key || opts.fileVar;
                opts.filename = filename || opts.filename;
            }
1191572
Bucket getBucketForKey(Object key)
    {
        int index = getBucketIndexForKey(key);

        Bucket bucket = buckets[index];
        if (bucket == null)
        {
            synchronized (bucketLock)
            {
                bucket = buckets[index];
            }
        }

        return bucket;
    }
330330
public void asyncPut(final byte[] data, final String key, final String token, StringMap params,
                         String mime, boolean checkCrc, UpCompletionHandler handler) throws IOException {
        checkArgs(key, data, null, token);
        if (mime == null) {
            mime = Client.DefaultMime;
        }
        params = filterParam(params);
        new FormUploader(client, token, key, data, params, mime, checkCrc, configuration).asyncUpload(handler);
    }
385067
def upload_file_to_bucket(self, bucket, file_path, key, is_public=False):
        

        with open(file_path, 'rb') as data:
            self.__s3.upload_fileobj(data, bucket, key)

        if is_public:
            self.__s3.put_object_acl(ACL='public-read', Bucket=bucket, Key=key)

        bucket_location = self.__s3.get_bucket_location(Bucket=bucket)
        file_url = "https://s3-{0}.amazonaws.com/{1}/{2}".format(
            bucket_location['LocationConstraint'],
            bucket,
            key)

        return file_url
290302
function upload (uploadables) {
  var promises = uploadables.map((uploadable) => putUploadable(uploadable))
  return Promise.all(promises)
}
283659
function upload(filesPaths) {
  const promises = filesPaths.map((v, i) => {
    const file = fs.createReadStream(v)
    const formData = {
      projectName: 'FrontEndResource',
      files: [file],
      path: 'frontend',
      usrName: '飞鱼籽',
    };
    return new Promise((resolve, reject) =>
      request.post({
        url: 'http://upload.2dfire-daily.com/upfileandlist',
        formData,
      }, function (error, response, body) {
        if (!error) {
          if (response.statusCode == 200) {
            try {
              const obj = JSON.parse(body)
              resolve({ file: v, response: obj })
              // console.log('success');
            } catch (error) {
              console.error(error);
            }
          } else {
            console.error('upload failed:', response.statusCode);
            reject()
          }
        } else {
          console.error('upload failed:', err);
          reject()
        }
      })
    )
  })

  return Promise.all(promises)
    .then((posts) => posts)
    .catch((e) => { console.error(e) });
}
1308191
function processFile(bucket, key, transform) {
  // get the csv file s3://${bucket}/${key}
  console.log(`Processing s3://${bucket}/${key}`)
  const s3 = new AWS.S3()
  const csvStream = csv.parse({ headers: true, objectMode: true })
  s3.getObject({Bucket: bucket, Key: key}).createReadStream().pipe(csvStream)
  return es.streamToEs(csvStream, transform, esClient, index)
}
4664
public function uploadFile($filePath, $key = null, $bucket = '')
    {
        if (!file_exists($filePath)) {
            throw new \Exception("上传的文件不存在",400);
        }
        $bucket = $bucket ? $bucket : $this->bucket;

        $uploadToken = $this->uploadToken(array('scope' => $bucket));
        $data = [];
        if (class_exists('\CURLFile')) {
            $data['file'] = new \CURLFile($filePath);
        } else {
            $data['file'] = '@' . $filePath;
        }
        $data['token'] = $uploadToken;
        if ($key) {
            $data['key'] = $key;
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->up_host);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec($ch);
        $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        $result = $this->response($result);
        if ($status == 200) {
            return $result;
        } else {
            throw new \Exception($result['error'],$status);
        }
    }
930142
def complete_multipart_upload(self, key_name, upload_id,
                                  xml_body, headers=None):
        
        query_args = 'uploadId=%s' % upload_id
        if headers is None:
            headers = {}
        headers['Content-Type'] = 'text/xml'
        response = self.connection.make_request('POST', self.name, key_name,
                                                query_args=query_args,
                                                headers=headers, data=xml_body)
        contains_error = False
        body = response.read()
        # Some errors will be reported in the body of the response
        # even though the HTTP response code is 200.  This check
        # does a quick and dirty peek in the body for an error element.
        if body.find('<Error>') > 0:
            contains_error = True
        boto.log.debug(body)
        if response.status == 200 and not contains_error:
            resp = CompleteMultiPartUpload(self)
            h = handler.XmlHandler(resp, self)
            xml.sax.parseString(body, h)
            return resp
        else:
            raise self.connection.provider.storage_response_error(
                response.status, response.reason, body)
780835
function (file, name) {
    //uploadFile(config.bucket, file.replace(/\\/g, '/'), name);
    
    

    //获取文件信息
    client.stat(config.bucket, name, function(err, ret) {

        if (!err) {
            //console.log(ret.hash, ret.fsize, ret.putTime, ret.mimeType);
            getEtag(file, function (hash) {
                
                if(hash != ret.hash){
                    // 不更新已存在的，忽略
                    if (!update_exist) {
                        log.i('Don\'t upload exist file: '.yellow + file);
                        return;
                    } 

                    need_upload_nums++;
                    if (scan_mode) return;
                    log.i('Need upload update file: '.yellow + file);
                    uploadFile(name, file);
                }

            });

        } else {

            // 文件不存在
            if(err.code == 612){
                need_upload_nums++;
                if (scan_mode) return;
                log.i('Need upload file: '.yellow + file);
                uploadFile(name, file);
            }else{
                log.e('get file stat err: '.cyan + name + '\n' + err);    
            }
        }
    });


    // var res = imagesBucket.key(name);
    // res.stat(function(err, stat) {
    //     if (err) {
    //         log.e('get file stat err: '.cyan + name + '\n' + err);
    //         return;
    //     }

    //     getEtag(file, function (hash) {
    //         //先判断七牛是否已存在文件
    //         if (stat.hash) {
    //             if (!update_exist) {
    //                 return;
    //             }
    //             if (stat.hash != hash) {
    //                 res.remove(function(err) {
    //                     if (err) {
    //                         return console.error(err);
    //                     }
    //                     need_upload_nums++;
    //                     if (scan_mode) return;
    //                     log.i('Need upload update file: '.yellow + file);
    //                 });
    //                 uploadFile(file,name);
    //             }
    //         } else {
    //             need_upload_nums++;
    //             if (scan_mode) return;
    //             log.i('Need upload file: '.yellow + file);
    //             uploadFile(file,name);
    //         }
    //     });
    // });
}
442694
function InitiateMultipartUpload(fileUpload, awsKey) {
            var request = {
                method: 'POST',
                path: '?uploads',
                step: 'initiate',
                x_amz_headers: fileUpload.xAmzHeadersAtInitiate,
                not_signed_headers: fileUpload.notSignedHeadersAtInitiate,
                response_match: '<UploadId>(.+)<\/UploadId>'
            };

            CancelableS3AWSRequest.call(this, fileUpload, request);
            this.awsKey = awsKey;
        }
1255612
function(callback) {
    var params = {
      Bucket: S3Handler.options.S3BucketName, 
      Key: S3Handler.options.mboxName + S3Handler.options.S3MboxKeySuffix, 
      Body: JSON.stringify(data)
    };
    
    s3.upload(params, function(err, response) {
      if (err) {
        if (CONSOLE_MESSAGES) console.log(new Date().toISOString(),'Error saving Mbox:',err);
      }
      callback(err, response);
    });
  }
434211
public function uploadToken(
        $bucket = null,
        $key = null,
        $expires = 3600,
        $policy = null)
    {
        // 默认使用当前配置的bucket
        if ($bucket === null) {
            $bucket = $this->bucket;
        }
        return $this->auth->uploadToken(
            $bucket,
            $key,
            $expires,
            $policy,
            true
        );
    }
623984
function writeFile(namespace, fileName, fileLocation) {
  var file;
  if (namespace) {
    file = path.join(namespace, fileName);
  } else {
    file = fileName;
  }
  var params = {
    localFile: fileLocation,
    ACL: CONSTANTS.AWS_BUCKET_PERMISSIONS,
    s3Params: {
      Bucket: config.bucket,
      Key: file
    }
  };
  var deferred = q.defer();
  var uploader = awsClient.uploadFile(params);
  uploader.on('error', function(err) {
    console.log(err);
    deferred.reject(err.stack);
  });
  uploader.on('end', function() {
    deferred.resolve(fileName);
  });
  return deferred.promise;
}