first commit
This commit is contained in:
commit
abb2b74468
8 files changed
+122
No files matched your search
+77
@@ -0,0 +1,77 @@
|
||||
(function (Scratch) {
|
||||
'use strict';
|
||||
|
||||
// 非沙箱守卫:确保扩展有权限发起网络请求
|
||||
if (!Scratch.extensions.unsandboxed) {
|
||||
throw new Error('S3 扩展必须运行在非沙箱模式下');
|
||||
}
|
||||
|
||||
class S3Extension {
|
||||
constructor() {
|
||||
// 在内存中保存凭据(仅用于演示,生产环境需注意安全)
|
||||
this.credentials = {};
|
||||
}
|
||||
|
||||
getInfo() {
|
||||
return {
|
||||
id: 's3client',
|
||||
name: 'S3 客户端',
|
||||
blocks: [
|
||||
{
|
||||
opcode: 'setCredentials',
|
||||
blockType: Scratch.BlockType.COMMAND,
|
||||
text: '设置 S3 凭据 Key [AK] Secret [SK] 区域 [REGION]',
|
||||
arguments: {
|
||||
AK: { type: Scratch.ArgumentType.STRING, defaultValue: 'AKIA...' },
|
||||
SK: { type: Scratch.ArgumentType.STRING, defaultValue: 'secret...' },
|
||||
REGION: { type: Scratch.ArgumentType.STRING, defaultValue: 'us-east-1' }
|
||||
}
|
||||
},
|
||||
{
|
||||
opcode: 'putObject',
|
||||
blockType: Scratch.BlockType.COMMAND,
|
||||
text: '上传桶 [BUCKET] 键 [KEY] 内容 [BODY]',
|
||||
arguments: {
|
||||
BUCKET: { type: Scratch.ArgumentType.STRING, defaultValue: 'my-bucket' },
|
||||
KEY: { type: Scratch.ArgumentType.STRING, defaultValue: 'hello.txt' },
|
||||
BODY: { type: Scratch.ArgumentType.STRING, defaultValue: 'Hello S3!' }
|
||||
}
|
||||
},
|
||||
{
|
||||
opcode: 'getObject',
|
||||
blockType: Scratch.BlockType.REPORTER,
|
||||
text: '下载桶 [BUCKET] 键 [KEY]',
|
||||
arguments: {
|
||||
BUCKET: { type: Scratch.ArgumentType.STRING, defaultValue: 'my-bucket' },
|
||||
KEY: { type: Scratch.ArgumentType.STRING, defaultValue: 'hello.txt' }
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
setCredentials(args) {
|
||||
this.credentials = {
|
||||
accessKey: args.AK,
|
||||
secretKey: args.SK,
|
||||
region: args.REGION
|
||||
};
|
||||
}
|
||||
|
||||
// 这里接入上一轮实现的 SigV4 签名与 fetch 调用
|
||||
async putObject(args) {
|
||||
const { accessKey, secretKey, region } = this.credentials;
|
||||
// 1. 计算 payload hash(对 BODY 做 SHA256)
|
||||
// 2. 构造 CanonicalRequest -> StringToSign -> Signature
|
||||
// 3. fetch(`https://${args.BUCKET}.s3.${region}.amazonaws.com/${args.KEY}`, { method: 'PUT', ... })
|
||||
// 返回成功或抛出错误
|
||||
}
|
||||
|
||||
async getObject(args) {
|
||||
// 签名时 payload hash 为空字符串的 SHA256
|
||||
// fetch GET,返回 res.text() 或 arrayBuffer
|
||||
}
|
||||
}
|
||||
|
||||
Scratch.extensions.register(new S3Extension());
|
||||
})(Scratch);
|
||||
Reference in new issue
Block a user