- 浏览: 1090224 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (290)
- php (65)
- javascript (36)
- html5 (31)
- thinkphp (9)
- mysql (16)
- jquery (13)
- node.js (9)
- css (9)
- android 开发 (8)
- flex (5)
- java (3)
- apache (8)
- linux (8)
- git (5)
- web (5)
- wordpress (9)
- mongodb (2)
- redis (5)
- yaf (6)
- python (4)
- big data (1)
- sphinx (1)
- html (1)
- bootstrap (1)
- vue (1)
- laravel (1)
- test (0)
最新评论
-
July01:
推荐用StratoIO打印控件,支持网页、URL、图片、PD、 ...
如何解决非IE浏览器的web打印 -
flashbehappy:
同一个视频,有mp4,ogg两种格式的。在chrome,fir ...
firefox chrom safari 对video标签的区别 -
xmdxzyf:
可以在网站(www.sosoapi.com)上试下在线表单方式 ...
用swagger-php/ui做API测试 -
flex_莫冲:
a2631500 写道"看了源码,设置Backbon ...
backbone与php交互 -
a2631500:
"看了源码,设置Backbone.emulateJS ...
backbone与php交互
upload 和putObject有什么区别,同样是上传或新增一个object
upload适用于比较大的文件,putObject适用于小的文件内容,upload支持自定义多线程并发上传
報錯:
2016-08-22 13:01:52 [ERROR] sync logs failed,err:BadRequest: An error occurred when parsing the HTTP request., file:/home/web/xxxx,file count:160
如果一個文件正在上傳,又重複執行了上傳操作,就會報上面這個錯誤。
upload适用于比较大的文件,putObject适用于小的文件内容,upload支持自定义多线程并发上传
var AWS = require('aws-sdk'); AWS.config.loadFromPath('./config/aws.config'); var fs = require('fs'); //var s3 = new AWS.S3({params:{Bucket:'xxx'}}); // OK // s3.listBuckets(function(error, data) { // if (error) { // console.log(error); // error is Response.error // } else { // console.log(data); // data is Response.data // } // }); // s3.getObject({Key:'download_test.log'}).on('success', function(response) { // console.log("Key was", response.request.params.Key); // }).send(); // var s3 = new AWS.S3(); // var params = {Bucket: 'mochong', Key:'message_center.txt'}; // var file = require('fs').createWriteStream('./deploy_local.txt'); // s3.getObject(params).createReadStream().pipe(file); /** create bucket 名字不能包含`_`等字符,必须唯一**/ /** var s3 = new AWS.S3(); var params = { Bucket: 'mochongmessagecenter', //required ACL: 'private', CreateBucketConfiguration: { LocationConstraint: 'us-west-2' } }; s3.createBucket(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response }); **/ /**delete bucket**/ /** * var s3 = new AWS.S3(); var params = { Bucket: 'mochongmessagecenter' //required }; s3.deleteBucket(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response }); **/ /** init multipart upload 开始多线程上传,获取uploadID**/ /** var params = { Bucket: 'mochong', //required Key: 'upload.txt', // required ACL: 'private', // CacheControl: 'STRING_VALUE', // ContentDisposition: 'STRING_VALUE', ContentEncoding: 'utf8', ContentLanguage: 'zh-cn', ContentType: 'plain', //Expires: new Date || 'Wed Dec 31 1969 16:00:00 GMT-0800 (PST)' || 123456789, //GrantFullControl: 'STRING_VALUE', // GrantRead: 'STRING_VALUE', //GrantReadACP: 'STRING_VALUE', //GrantWriteACP: 'STRING_VALUE', //Metadata: { // someKey: 'STRING_VALUE', // anotherKey: ... //}, //RequestPayer: 'requester', //SSECustomerAlgorithm: 'STRING_VALUE', //SSECustomerKey: new Buffer('...') || 'STRING_VALUE', // SSECustomerKeyMD5: 'STRING_VALUE', //SSEKMSKeyId: 'STRING_VALUE', //ServerSideEncryption: 'AES256 | aws:kms', //StorageClass: 'STANDARD | REDUCED_REDUNDANCY | STANDARD_IA', //WebsiteRedirectLocation: 'STRING_VALUE' }; var s3 = new AWS.S3(); // { // Bucket: 'mochong', // Key: 'upload.txt', // UploadId: '3jNsFCZRDwASLGpMi2cYBUt4bieR8hMj3F7CAoDZkWrpAimkB95FLx.ZRmfkCGQZL2V3KVU7HkmMlWsATIH.hcWPj7VQ_cu8.lKtpEk8TbBF60Z93bKAUE3Duo5d1GaB' // } s3.createMultipartUpload(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response }); **/ /**开始多线程上传**/ // var s3 = new AWS.S3(); // fs.readFile('./download_test.log', 'utf8', function (err,data) { // if (err) { // return console.log(err); // } // console.log(data); // var params = { // Bucket: 'mochong', // required // Key: 'upload.txt', // required // PartNumber: 5, // 1~10000 // UploadId: '3jNsFCZRDwASLGpMi2cYBUt4bieR8hMj3F7CAoDZkWrpAimkB95FLx.ZRmfkCGQZL2V3KVU7HkmMlWsATIH.hcWPj7VQ_cu8.lKtpEk8TbBF60Z93bKAUE3Duo5d1GaB', /* required */ // Body: data, // ContentLength: Buffer.byteLength(data, 'utf8'), // //ContentMD5: 'STRING_VALUE', // //RequestPayer: 'requester', // // SSECustomerAlgorithm: 'STRING_VALUE', // // SSECustomerKey: new Buffer('...') || 'STRING_VALUE', // //SSECustomerKeyMD5: 'STRING_VALUE' // }; // s3.uploadPart(params, function(err, data) { // if (err) console.log(err, err.stack); // an error occurred // else console.log(data); // successful response { ETag: '"6f9769150b95562cd1831f82e7026338"' } // }); // }); /**结束上传**/ /* var s3 = new AWS.S3(); var params = { Bucket: 'mochong', // required Key: 'upload.txt', // required UploadId: '3jNsFCZRDwASLGpMi2cYBUt4bieR8hMj3F7CAoDZkWrpAimkB95FLx.ZRmfkCGQZL2V3KVU7HkmMlWsATIH.hcWPj7VQ_cu8.lKtpEk8TbBF60Z93bKAUE3Duo5d1GaB', // required MultipartUpload: { Parts: [ { ETag: '6f9769150b95562cd1831f82e7026338', PartNumber: 5 // must >= 1 }, ] }, RequestPayer: 'requester' }; s3.completeMultipartUpload(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response // response data: //{ Location: 'https://mochong.s3-us-west-2.amazonaws.com/upload.txt', // Bucket: 'mochong', // Key: 'upload.txt', // ETag: '"0598c44b0e25dde9e8dfdb55043b3a5f-1"' } });*/ /**忽略上传**/ /* var s3 = new AWS.S3(); var params = { Bucket: 'mochong', //required Key: 'upload.txt', //required UploadId: '3jNsFCZRDwASLGpMi2cYBUt4bieR8hMj3F7CAoDZkWrpAimkB95FLx.ZRmfkCGQZL2V3KVU7HkmMlWsATIH.hcWPj7VQ_cu8.lKtpEk8TbBF60Z93bKAUE3Duo5d1GaB', //required RequestPayer: 'requester' }; s3.abortMultipartUpload(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response: {} });*/ /**智能上传 要注意必须用stream,buffer的类型**/ //var s3 = new AWS.S3(); // 不要用readFile.因为 s3的upload只支持stream,buffer,blob //Uploads an arbitrarily sized buffer, blob, or stream, //using intelligent concurrent handling of parts if the payload is large enough. //You can configure the concurrent queue size by setting options. // Note that this is the only operation for which the SDK can retry requests with stream bodies. /* var list = []; var fileReadStream = fs.createReadStream('./log/api.log', {encoding: 'utf8'}); var tmpBuffer; fileReadStream.on('data', function (chunk) { tmpBuffer = new Buffer(chunk); list.push(tmpBuffer); }).on('end', function () { fileReadStream.destroy(); var stream = Buffer.concat(list); var params = {Bucket: 'mochong', Key: 'api.log', Body: stream}; //var options = {partSize: 1 * 1024 * 1024, queueSize: 1}; //queueSize:骞跺彂鏁帮紝partSize锛氬崟鍧椾笂浼犲ぇ灏�,must >=5242880 var upload = s3.upload(params, function(err, data) { if (err) { console.log(err); } }); upload.send(function (err, data) { stream.fill(); if (err) console.log("Error:", err.message); else console.log(data); // { ETag: '"3c6ecbcf8d6b65ccd16b47f603bff1b2"', // Location: 'https://mochong.s3-us-west-2.amazonaws.com/download_test.log', // key: 'download_test.log', // Key: 'download_test.log', // Bucket: 'mochong' // } }); }).on('close', function (err) { console.log('Stream has been destroyed and file has been closed'); tmpBuffer.fill(); });*/ /** list objectV2 *Returns some or all (up to 1000) of the objects in a bucket. * You can use the request parameters as selection criteria to return a subset of the objects in a bucket. * Note: ListObjectsV2 is the revised List Objects API and we recommend you use this revised API for new application development. * **/ /* var params = { Bucket: 'mochong', // required Delimiter: '|', EncodingType: 'url', //Marker: 'STRING_VALUE', // MaxKeys: 0, // Prefix: 'STRING_VALUE' Prefix: 'message_center/', StartAfter: 'STRING_VALUE'//StartAfter is where you want Amazon S3 to start listing from. Amazon S3 starts listing after this specified key. StartAfter can be any key in the bucket }; var s3 = new AWS.S3(); s3.listObjectsV2(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response });*/ /** *response data: *{ IsTruncated: false, Marker: 'STRING_VALUE', Contents: [], Name: 'mochong', Prefix: 'STRING_VALUE', Delimiter: '%7C', MaxKeys: 0, CommonPrefixes: [], EncodingType: 'url' } */ /** get object**/ /* var s3 = new AWS.S3(); var params = { Bucket: 'mochong', //required Key: 'message_center/deploy_local.txt', // required //IfMatch: 'STRING_VALUE',// (String) Return the object only if its entity tag (ETag) is the same as the one specified, otherwise return a 412 (precondition failed). //IfModifiedSince: // new Date || 'Wed Dec 31 1969 16:00:00 GMT-0800 (PST)' || 123456789, //IfNoneMatch: 'STRING_VALUE',//(String) Return the object only if its entity tag (ETag) is different from the one specified, otherwise return a 304 (not modified). IfUnmodifiedSince:1471536000,// new Date || 'Wed Dec 31 1969 16:00:00 GMT-0800 (PST)' || 123456789, //Range: 'STRING_VALUE', //RequestPayer: 'requester', //ResponseCacheControl: 'STRING_VALUE', //ResponseContentDisposition: 'STRING_VALUE', //ResponseContentEncoding: 'STRING_VALUE', //ResponseContentLanguage: 'STRING_VALUE', //ResponseContentType: 'STRING_VALUE', //ResponseExpires: new Date || 'Wed Dec 31 1969 16:00:00 GMT-0800 (PST)' || 123456789, //SSECustomerAlgorithm: 'STRING_VALUE', //SSECustomerKey: new Buffer('...') || 'STRING_VALUE', //SSECustomerKeyMD5: 'STRING_VALUE', //VersionId: 'STRING_VALUE' }; s3.getObject(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response }); */ /* success: { AcceptRanges: 'bytes', LastModified: 'Thu, 18 Aug 2016 06:49:12 GMT', ContentLength: '0', ETag: '"d41d8cd98f00b204e9800998ecf8427e"', ContentType: 'text/plain', Metadata: {}, Body: <Buffer > } error: { [PreconditionFailed : At least one of the pre - conditions you specified did not hold] message : 'At least one of the pre-conditions you specified did not hold', code : 'PreconditionFailed', region : null, time : Fri Aug 19 2016 10 : 06 : 38 GMT + 0800(CST), requestId : 'D57010FB7BAD5DD3', extendedRequestId : 'GrAFqim56ytBONLN7oTj+Idhui6UlK66UfJ1GuP5X/aNocDpoufpOrRCdaxct3uUNXA/MBHIQIQ=', cfId : undefined, statusCode : 412, retryable : false, retryDelay : 38.56445546261966 } 'PreconditionFailed: At least one of the pre-conditions you specified did not hold\n at Request.extractError (/home/web/message_center/node_modules/aws-sdk/lib/services/s3.js:538:35)\n at Request.callListeners (/home/web/message_center/node_modules/aws-sdk/lib/sequential_executor.js:105:20)\n at Request.emit (/home/web/message_center/node_modules/aws-sdk/lib/sequential_executor.js:77:10)\n at Request.emit (/home/web/message_center/node_modules/aws-sdk/lib/request.js:661:14)\n at Request.transition (/home/web/message_center/node_modules/aws-sdk/lib/request.js:22:10)\n at AcceptorStateMachine.runTo (/home/web/message_center/node_modules/aws-sdk/lib/state_machine.js:14:12)\n at /home/web/message_center/node_modules/aws-sdk/lib/state_machine.js:26:10\n at Request.<anonymous> (/home/web/message_center/node_modules/aws-sdk/lib/request.js:38:9)\n at Request.<anonymous> (/home/web/message_center/node_modules/aws-sdk/lib/request.js:663:12)\n at Request.callListeners (/home/web/message_center/node_modules/aws-sdk/lib/sequential_executor.js:115:18)' */ /* { AcceptRanges: 'bytes', LastModified: 'Thu, 18 Aug 2016 07:22:22 GMT', ContentLength: '23', ETag: '"0598c44b0e25dde9e8dfdb55043b3a5f-1"', ContentEncoding: 'utf-8', ContentLanguage: 'zh-cn', ContentType: 'plain', Metadata: {}, Body: <Buffer 54 68 69 73 20 69 73 20 61 20 66 69 6c 65 20 74 6f 20 74 65 73 74 2e> } */ /** put object * @info Adds an object to a bucket. ***/ var s3 = new AWS.S3(); var crypto = require('crypto'); var body = 'pull by put object api2'; var md5 = crypto.createHash('md5'); var contentMd5 = md5.update(body).digest('base64'); var params = { Bucket: 'mochong', //required Key: 'new_folder/file_put_object.txt', // required ACL: 'private', Body: body, //CacheControl: 'STRING_VALUE', //ContentDisposition: 'STRING_VALUE', ContentEncoding: 'utf8', ContentLanguage: 'zh-cn', ContentLength: body.length, ContentMD5: contentMd5, ContentType: 'text/plain', //Expires: new Date || 'Wed Dec 31 1969 16:00:00 GMT-0800 (PST)' || 123456789, //GrantFullControl: 'STRING_VALUE', //GrantRead: 'STRING_VALUE', //GrantReadACP: 'STRING_VALUE', //GrantWriteACP: 'STRING_VALUE', Metadata: { 'x-amz-meta-source-type': 'server',// 自定义的metadata必须用x-amz-meta-做前缀 'x-amz-meta-author': 'mochong' /* anotherKey: ... */ }, //RequestPayer: 'requester', //SSECustomerAlgorithm: 'STRING_VALUE', //SSECustomerKey: new Buffer('...') || 'STRING_VALUE', //SSECustomerKeyMD5: 'STRING_VALUE', //SSEKMSKeyId: 'STRING_VALUE', // ServerSideEncryption: 'AES256 | aws:kms', StorageClass: 'STANDARD', //WebsiteRedirectLocation: 'STRING_VALUE' }; s3.putObject(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response }); /** * response: *{ ETag: '"e615fa17312c3e6e8a1682a5ce964d21"' } */
function syncFile(file) { var bodyStream = fs.createReadStream(file.absolute_path, {encoding: 'utf8'}); var options = { Bucket : config.aws.bucket, Key : config.aws.key_prefix + "/" + file.relative_path, ContentLength : file.size, Body : bodyStream }; s3.putObject(options, function(err, data) { if (err) { console.log("upload err,filepath:" + file.absolute_path); console.log(err); process.exit(); return false; } bodyStream.destroy(); }); }
報錯:
2016-08-22 13:01:52 [ERROR] sync logs failed,err:BadRequest: An error occurred when parsing the HTTP request., file:/home/web/xxxx,file count:160
如果一個文件正在上傳,又重複執行了上傳操作,就會報上面這個錯誤。
发表评论
-
在线自动协同编辑文本的实现方案
2016-10-14 16:49 1978要实现可以自动在线协同编辑文本的功能。就类似google do ... -
winston log split by date的解决方案
2015-11-25 11:56 1328winston 是node.js的日志管理工具,但是它的api ... -
nodejs程序后台一直运行
2015-10-22 09:57 2867来源:http://materliu.github ... -
解决nodemailer发送email提示Invalid login 的问题
2015-09-24 10:57 1833上代码 var nodemailer = require ... -
node.js获取php curl post数据req.body为空的处理
2015-09-22 11:43 3965node使用了express4和body-parser来解析p ... -
kue api文档说明
2015-08-12 16:56 3414## 创建job queue job队列 va ... -
node.js的异常处理-domain
2015-08-12 15:41 421参考: https://cnodejs.org/topic/5 ... -
<了不起的NODEJS>学习笔记
2015-06-13 21:57 430一、 Chapter1 package.json { &qu ...
相关推荐
适用于 Node.js 示例项目的 AWS 开发工具包 一个简单的 Node.js 应用程序,说明了适用于 Node.js 的 AWS 开发工具包的用法。 要求 此应用程序的唯一要求是节点包管理器。 所有其他依赖项(包括适用于 Node.js 的 AWS...
首先,我们来了解一下AWS的核心服务之一——Amazon Elastic Compute Cloud (EC2)。EC2提供了可伸缩的计算能力,允许用户在云中启动和管理虚拟服务器。在aws-nodejs-sample中,开发者可能使用EC2实例来托管他们的Node...
总结,本篇文章介绍了如何使用Node.js和AWS SDK将文件上传到S3。这个过程涉及到配置AWS凭证、创建S3实例、定义上传函数以及设置一个简单的HTTP服务器来处理文件上传请求。这个基础架构可以进一步扩展以满足更复杂的...
适用于JavaScript v3的AWS开发工具包 适用于JavaScript v3的AWS开发工具包是对v2的重写,具有一些出色的新功能。 与版本2一样,它使您可以轻松地使用 ,但具有,其中每个服务都有单独的程序包。 它还包括许多经常...
特征将本地文件系统与远程 Amazon S3 存储桶同步将远程 Amazon S3 存储桶与本地文件系统同步同步两个远程 Amazon S3 存储桶仅同步新的和更新的对象支持 AWS CLI 选项--delete , --dryrun 支持 AWS SDK 命令输入选项...
描述中提到的“nodejs-aws-s3-sample”可能是一个GitHub仓库或者下载包,包含了用Node.js编写的具体代码示例,用于演示如何使用AWS SDK来操作S3服务。这个项目可能是为了帮助开发者学习如何上传、下载、列出S3存储桶...
该SDK通过为包括Amazon S3,Amazon EC2,DynamoDB和Amazon SWF在内的AWS服务提供JavaScript对象,帮助消除了复杂性。 单个可下载的软件包包括AWS JavaScript库和文档。 将此包添加到您的应用程序中会将AWS对象添加...
要安装Composer和SDK,请运行: curl -sS https://getcomposer.org/installer | php php composer.phar install 基本配置 您需要先设置AWS安全凭证,然后示例代码才能连接到AWS。 为此,您可以在〜/ .aws /(对于...
aws4 一个小型实用程序,用于使用Amazon的签署普通的Node.js http(s)请求选项。 如果要在浏览器或类的环境中签名和发送AWS请求,请签出 –否则,您还可以捆绑此库以使用。 截至2020-05-22,唯一不支持v4的AWS...
我们可能还会发现一些示例代码,展示如何使用AWS SDK调用不同服务,如S3用于对象存储,DynamoDB用于NoSQL数据库操作,或者使用CloudWatch进行日志管理和监控。 此外,教程可能还会涵盖错误处理、安全性最佳实践,如...
使用 AWS SDK for JavaScript,开发者可以轻松地通过 Node.js 代码与 S3 进行交互,执行各种操作,如创建、读取、更新和删除对象。 **Amazon SQS:队列服务** SQS 是 AWS 的一个无服务器、完全托管的消息队列服务...
用于下载 Amazon S3 存储桶的简单 NodeJS 脚本 设置 克隆项目: git clone git@github.com:woganmay/s3-download-bucket.git cd s3-download-bucket/ 安装所需的节点模块: npm install aws-sdk minimist 通过在...
Node.js的`aws-sdk`库提供了对S3的全面支持,包括上传流。我们可以通过创建一个`AWS.S3`对象,然后调用`createReadStream`方法从iOS客户端接收流,接着使用`createWriteStream`方法将接收到的流写入S3。这样,文件在...
简单的AWS S3包装器 :fire: 安装 $ npm install s3-bucket 环境变量 S3_BUCKET_ACCESS_KEY_ID=value S3_BUCKET_SECRET_ACCESS_KEY=value S3_BUCKET_NAME=value S3_BUCKET_REGION=value 用法 // Don't forgot to ...
AWS S3图像上传器s3-uploader@1.1文档可以在找到。 灵活高效地调整图像大小,重命名并上传到Amazon S3磁盘存储。 使用官方的以及和进行图像处理。变更日志所有更改都记录在。 最新版本的更改可以在。安装npm install...
在高端台式机CPU上,单个nodejs线程可以处理> 1GB / s 客户端连接 将客户端连接到本地s3模拟服务器 node src/client http://localhost:8080 结果 偏僻的 两台计算机直接用8M cat6网络电缆连接。 客户 CPU:16核心
一个简单的nodeJS脚本,使用“ aws-sdk” NPM模块。 我之所以建立它,是因为在S3上我有一个装满图像的巨大存储桶,并且需要向所有这些图像添加缓存控制标头。 AWS S3(目前)不支持在存储桶级别添加缓存控制,因此...
const s3 = new AWS.S3({ accessKeyId: 'YOUR_ACCESS_KEY', secretAccessKey: 'YOUR_SECRET_KEY', region: 'YOUR_S3_REGION', }); const uploadImage = async (imageUri, width, height) => { const ...
2. **AWS服务集成**:如使用AWS SDK for JavaScript与AWS服务交互,可能涉及到的AWS服务有DynamoDB(数据库)、S3(存储)、Lambda(无服务器计算)、API Gateway(API托管)等。 3. **TypeScript进阶**:学习如何...
- `package.json`: 项目依赖的配置文件,列出了所有必需的npm模块,如`aws-sdk`用于与AWS服务交互。 - `.travis.yml`: Travis CI的配置文件,定义了构建和部署的流程。 - `index.js`或类似的入口文件:包含应用程序...