-
Notifications
You must be signed in to change notification settings - Fork 568
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat 1、提升安全性:不再支持path-style 2、修复 uploadFile 未对参数 Key 进行校验
- Loading branch information
Showing
10 changed files
with
349 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "cos-js-sdk-nextjs", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"dev": "next", | ||
"build": "next build", | ||
"start": "next start", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"cos-js-sdk-v5": "^1.5.0", | ||
"next": "^14.0.4", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import COS from 'cos-js-sdk-v5'; | ||
|
||
// 初始化可参考 https://cloud.tencent.com/document/product/436/11459#.E5.BC.80.E5.A7.8B.E4.BD.BF.E7.94.A8 | ||
const cos = new COS({ | ||
SecretId: '', | ||
SecretKey: '', | ||
}); | ||
|
||
let selectedFile = null; | ||
|
||
const handleFileChange = (event) => { | ||
selectedFile = event.target.files[0]; | ||
}; | ||
|
||
const handleUpload = () => { | ||
if (selectedFile) { | ||
console.log('上传文件:', selectedFile); | ||
cos.uploadFile({ | ||
Bucket: 'examplebucket-1250000000', /* 填入您自己的存储桶,必须字段 */ | ||
Region: 'COS_REGION', /* 存储桶所在地域,例如ap-beijing,必须字段 */ | ||
Key: selectedFile.name, /* 存储在桶里的对象键(例如1.jpg,a/b/test.txt),必须字段 */ | ||
Body: selectedFile, /* 必须,上传文件对象,可以是input[type="file"]标签选择本地文件后得到的file对象 */ | ||
SliceSize: 1024 * 1024 * 5, /* 触发分块上传的阈值,超过5MB使用分块上传,非必须 */ | ||
onProgress: function (progressData) { /* 非必须 */ | ||
console.log(JSON.stringify(progressData)); | ||
}, | ||
}, function (err, data) { | ||
console.log(err || data); | ||
}); | ||
|
||
} else { | ||
console.log('请选择一个文件'); | ||
} | ||
}; | ||
|
||
export default () => <div> | ||
<input type="file" onChange={handleFileChange} /> | ||
<button onClick={handleUpload}>上传</button> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.