(options)
| 404 | } |
| 405 | |
| 406 | function putOssObject(options) { |
| 407 | const endpoint = normalizeEndpoint(options.endpoint); |
| 408 | const objectPath = `/${encodeObjectKey(options.objectKey)}`; |
| 409 | const hostname = `${options.bucket}.${endpoint.hostname}`; |
| 410 | const date = new Date().toUTCString(); |
| 411 | const contentType = options.contentType || "application/octet-stream"; |
| 412 | const headers = { |
| 413 | Date: date, |
| 414 | "Content-Type": contentType, |
| 415 | "Content-Length": options.body.length, |
| 416 | Authorization: ossAuthorization({ |
| 417 | method: "PUT", |
| 418 | bucket: options.bucket, |
| 419 | objectKey: options.objectKey, |
| 420 | accessKeyId: options.accessKeyId, |
| 421 | accessKeySecret: options.accessKeySecret, |
| 422 | date, |
| 423 | contentType, |
| 424 | }), |
| 425 | }; |
| 426 | |
| 427 | const requestOptions = { |
| 428 | method: "PUT", |
| 429 | protocol: endpoint.protocol, |
| 430 | hostname, |
| 431 | path: objectPath, |
| 432 | headers, |
| 433 | }; |
| 434 | |
| 435 | const transport = endpoint.protocol === "http:" ? http : https; |
| 436 | |
| 437 | return new Promise((resolve, reject) => { |
| 438 | const request = transport.request(requestOptions, (response) => { |
| 439 | const chunks = []; |
| 440 | response.on("data", (chunk) => chunks.push(chunk)); |
| 441 | response.on("end", () => { |
| 442 | if (response.statusCode >= 200 && response.statusCode < 300) { |
| 443 | resolve(); |
| 444 | return; |
| 445 | } |
| 446 | const body = Buffer.concat(chunks).toString("utf8").trim(); |
| 447 | reject(new Error(`OSS upload failed with HTTP ${response.statusCode}${body ? `: ${body}` : ""}`)); |
| 448 | }); |
| 449 | }); |
| 450 | request.on("error", reject); |
| 451 | request.write(options.body); |
| 452 | request.end(); |
| 453 | }); |
| 454 | } |
| 455 | |
| 456 | function normalizeEndpoint(endpoint) { |
| 457 | const value = endpoint.startsWith("http://") || endpoint.startsWith("https://") |
no test coverage detected