@blueshit/oss-client
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -20,2 +20,12 @@ /** | ||
export declare type METHOD = "PUT" | "GET" | "POST" | "HEAD" | "DELETE"; | ||
export interface IHeader { | ||
[header: string]: number | string | string[] | undefined; | ||
} | ||
export interface IReply { | ||
code: number; | ||
headers: IHeader; | ||
buffer?: Buffer; | ||
message?: string; | ||
body?: string; | ||
} | ||
export default class OSSClient { | ||
@@ -31,3 +41,7 @@ private accessKeyId; | ||
sign(method: METHOD, md5: string, contentType: string, date: string, name: string): string; | ||
putObject(key: string, data: Buffer | Readable): Promise<{}>; | ||
private requestObject; | ||
putObject(key: string, data: Buffer | Readable): Promise<IReply>; | ||
getObject(key: string): Promise<IReply>; | ||
deleteObject(key: string): Promise<IReply>; | ||
objectMeta(key: string): Promise<IReply>; | ||
} |
@@ -30,3 +30,3 @@ "use strict"; | ||
} | ||
request(params, data) { | ||
request(params, data, raw = false) { | ||
// eslint-disable-next-line | ||
@@ -38,9 +38,16 @@ return new Promise((resolve, reject) => { | ||
response.on("end", () => { | ||
const res = Buffer.concat(buffers).toString("utf8"); | ||
if (response.statusCode === 200) | ||
return resolve(res); | ||
return reject({ httpcode: response.statusCode, code: response.statusCode, message: res }); | ||
const buf = Buffer.concat(buffers); | ||
if (response.statusCode === 200) { | ||
return resolve({ | ||
code: response.statusCode, | ||
headers: response.headers, | ||
buffer: buf, | ||
body: raw ? "" : buf.toString("utf8") | ||
}); | ||
} | ||
const res = buf.toString("utf8"); | ||
return reject({ code: response.statusCode, headers: response.headers, message: res }); | ||
}); | ||
response.on("error", e => { | ||
return reject({ httpcode: response.statusCode, code: response.statusCode, message: "" + e }); | ||
return reject({ code: response.statusCode, headers: response.headers, message: "" + e }); | ||
}); | ||
@@ -66,10 +73,10 @@ }); | ||
} | ||
putObject(key, data) { | ||
requestObject(method, key, data, raw = false) { | ||
const date = new Date().toUTCString(); | ||
const fielkey = this.prefix ? this.prefix + key : key; | ||
const sign = this.sign("PUT", "", "", date, fielkey); | ||
const sign = this.sign(method, "", "", date, fielkey); | ||
const option = { | ||
hostname: `${this.bucket}.${this.endpoint}`, | ||
path: `/${fielkey}`, | ||
method: "PUT", | ||
method: method, | ||
headers: { | ||
@@ -80,6 +87,18 @@ Date: date, | ||
}; | ||
return this.request(option, data); | ||
return this.request(option, data, raw); | ||
} | ||
putObject(key, data) { | ||
return this.requestObject("PUT", key, data); | ||
} | ||
getObject(key) { | ||
return this.requestObject("GET", key, undefined, true); | ||
} | ||
deleteObject(key) { | ||
return this.requestObject("DELETE", key + "?objectMeta"); | ||
} | ||
objectMeta(key) { | ||
return this.requestObject("GET", key + "?objectMeta"); | ||
} | ||
} | ||
exports.default = OSSClient; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@blueshit/oss-client", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "简单阿里云OSS客户端", | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts", | ||
"files": [ | ||
"dist/index.js", | ||
"dist/index.d.ts" | ||
], | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"test": "ts-node src/test.ts", | ||
"prepublishOnly": "tsc" | ||
@@ -9,0 +14,0 @@ }, |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
1
7546
5
144