Skip to content

Commit d5ce9cd

Browse files
summerboy2134liucheng.yao
andauthored
Add PHP-SDK Content (#27)
* Consolidated changes * Consolidated changes --------- Co-authored-by: liucheng.yao <[email protected]>
1 parent 2e21768 commit d5ce9cd

22 files changed

+1792
-42
lines changed

_config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,5 @@ collections:
249249
output: true
250250
node.js-sdk:
251251
output: true
252+
php-sdk:
253+
output: true

_data/navigation.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,46 @@ node.js-sdk:
350350
url: /node.js-sdk/S3协议常用工具.html
351351

352352

353+
354+
php-sdk:
355+
- title: PHP-SDK
356+
children:
357+
- title: 概述
358+
url: /php-sdk/概述.html
359+
- title: 安装
360+
url: /php-sdk/安装.html
361+
- title: 快速使用
362+
url: /php-sdk/快速使用.html
363+
364+
- title: 文件上传
365+
children:
366+
- title: 简单上传
367+
url: /php-sdk/简单上传.html
368+
- title: 分片上传
369+
url: /php-sdk/分片上传.html
370+
- title: 指定存储类型上传
371+
url: /php-sdk/指定存储类型上传.html
372+
- title: 文件管理
373+
children:
374+
- title: 存储类型转换
375+
url: /php-sdk/存储类型转换.html
376+
- title: 获取对象权限信息
377+
url: /php-sdk/获取对象权限信息.html
378+
- title: 获取对象元数据
379+
url: /php-sdk/获取对象元数据.html
380+
- title: 获取目录文件列表
381+
url: /php-sdk/获取目录文件列表.html
382+
- title: 文件下载
383+
url: /php-sdk/文件下载.html
384+
- title: 删除文件
385+
url: /php-sdk/删除文件.html
386+
- title: 拷贝文件
387+
url: /php-sdk/拷贝文件.html
388+
- title: 解冻归档文件
389+
url: /php-sdk/解冻归档文件.html
390+
- title: AWS S3 协议支持说明
391+
url: /php-sdk/S3协议支持说明.html
392+
- title: AWS S3 协议常用工具
393+
url: /php-sdk/S3协议常用工具.html
394+
395+
-59.6 KB
Binary file not shown.

collections/_node.js-sdk/分片上传.md

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,31 @@ sidebar:
2525
### 示例
2626
> 执行该示例前请确保配置文件的正确性<br>以下代码段需要在上下文中运行
2727
28-
<div class="copyable" markdown="1">
28+
<div class="copyable" markdown="1">
2929
{% highlight javascript linenos %}
3030
const {
3131
CreateMultipartUploadCommand,
3232
UploadPartCommand,
3333
CompleteMultipartUploadCommand,
3434
AbortMultipartUploadCommand
3535
} = require("@aws-sdk/client-s3");
36-
const s3 = require('./s3client');
36+
const s3 = require('../s3client');
3737
const fs = require('fs');
38-
const crypto = require('crypto');
38+
const crypto = require('crypto');
3939

4040
// 计算 MD5
4141
const calculateMD5 = (data) => {
42-
return crypto.createHash('md5').update(data).digest('base64');
42+
return crypto.createHash('md5').update(data).digest('base64');
4343
};
4444

4545
const sliceUpload = async (bucketName, key, filePath) => {
46-
const fileBuffer = fs.readFileSync(filePath);
47-
const partSize = 8 * 1024 * 1024; // 固定8MB
48-
const numParts = Math.ceil(fileBuffer.length / partSize);
49-
46+
const fileBuffer = fs.readFileSync(filePath);
47+
const partSize = 8 * 1024 * 1024; // 固定8MB
48+
const numParts = Math.ceil(fileBuffer.length / partSize);
49+
5050
let uploadId;
5151

52-
try {
53-
52+
try {
5453
// 创建分片上传任务
5554
const createMultipartUploadResponse = await s3.send(
5655
new CreateMultipartUploadCommand({
@@ -60,15 +59,13 @@ const sliceUpload = async (bucketName, key, filePath) => {
6059
);
6160

6261
uploadId = createMultipartUploadResponse.UploadId;
63-
const uploadPromises = [];
64-
65-
// 分片上传
62+
const uploadedParts = [];
63+
6664
for (let partNumber = 1; partNumber <= numParts; partNumber++) {
6765
const start = (partNumber - 1) * partSize;
6866
const end = Math.min(start + partSize, fileBuffer.length);
6967
const partBuffer = fileBuffer.subarray(start, end);
7068
const md5Hash = calculateMD5(partBuffer);
71-
7269
const uploadPartCommand = new UploadPartCommand({
7370
Bucket: bucketName,
7471
Key: key,
@@ -78,22 +75,21 @@ const sliceUpload = async (bucketName, key, filePath) => {
7875
ContentMD5: md5Hash,
7976
});
8077

81-
uploadPromises.push(
82-
s3.send(uploadPartCommand).then((uploadPartResponse) => ({
83-
ETag: uploadPartResponse.ETag,
84-
PartNumber: partNumber,
85-
}))
86-
);
78+
const uploadPartResponse = await s3.send(uploadPartCommand);
79+
console.log(`Part ${partNumber} uploaded successfully`);
80+
uploadedParts.push({
81+
ETag: uploadPartResponse.ETag,
82+
PartNumber: partNumber,
83+
});
8784
}
88-
const uploadedParts = await Promise.all(uploadPromises);
89-
90-
// 验证每个分片是否正确上传
85+
86+
// 验证分片
9187
uploadedParts.forEach((part) => {
9288
if (!part.ETag) {
9389
throw new Error(`Part ${part.PartNumber} failed to upload.`);
9490
}
95-
});
96-
91+
});
92+
9793
// 完成分片上传
9894
await s3.send(
9995
new CompleteMultipartUploadCommand({
@@ -110,9 +106,8 @@ const sliceUpload = async (bucketName, key, filePath) => {
110106
} catch (error) {
111107
console.error("Error uploading file:", error);
112108

113-
if (uploadId) {
114-
115-
// 如果上传失败,可以选择在此处中止上传
109+
if (uploadId) {
110+
// 中止上传
116111
await s3.send(
117112
new AbortMultipartUploadCommand({
118113
Bucket: bucketName,
@@ -123,12 +118,12 @@ const sliceUpload = async (bucketName, key, filePath) => {
123118
}
124119
}
125120
};
126-
{% endhighlight %}
127-
</div>
121+
{% endhighlight %}
122+
</div>
128123

129124
> `Example/` 目录中运行以下命令执行该示例
130-
<div class="copyable" markdown="1">
125+
<div class="copyable" markdown="1">
131126
{% highlight bash linenos %}
132127
$ node SliceUpload.js <bucketName> <keyName> <filePath>
133-
{% endhighlight %}
128+
{% endhighlight %}
134129
</div>

collections/_node.js-sdk/快速使用.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@ sidebar:
55
---
66

77
## 快速运行 SDK 示例
8-
### 1. 准备工作
9-
1. 登录[对象存储控制台](https://console.ucloud.cn/ufile/ufile),创建存储空间。获取存储空间名称和存储空间域名。
10-
2. 登录[账号管理控制台](https://console.ucloud.cn/uaccount/api_manage),获取您的项目公钥和私钥。
11-
3. 配置CORS 规则,来源Origin可以按需配置,Allow-Header 需配成*,Expose-Headers 需要 ETag、Content-Length 以及其他 js 需要读取的 header 字段,如下图所示。
12-
![image-cors](img/cors.png)
138

14-
### 2. 配置s3客户端文件
15-
确保已完成跨域设置,修改s3client.js 文件
9+
### 1. 配置s3客户端文件
10+
在项目根目录下,修改s3client.js 文件
1611

1712

1813
<div class="copyable" markdown="1">
@@ -47,9 +42,9 @@ module.exports = s3;
4742
| signatureVersion | AWS签名版本,默认使用签名版本 4 来验证请求 | String ||
4843
| forcePathStyle | 使用路径风格或虚拟主机风格,具体参考 AWS S3 协议支持说明 | Boolean ||
4944

50-
### 3. 运行示例
45+
### 2. 运行示例
5146
首先确保已完成SDK示例的本地安装。
52-
以文件上传为例,必须的输入参数为配置文件对应地域下的`Bucket Name`,以及需要保存在该Bucket 下的`Key Name` (即文件名) ,存储类型默认为标准存储。若要指定存储类型,参照**S3协议支持说明**,确认完输入信息后点击上传文件。
47+
以文件上传为例,必须的输入参数为配置文件对应地域下的`bucketName`,需要保存在该Bucket 下的`keyname` (即文件名) ,以及待上传的文件的本地路径`filePath`,存储类型默认为标准存储。若要指定存储类型,参照**S3协议支持说明**,确认完输入信息后点击上传文件。
5348

5449
<div class="copyable" markdown="1">
5550
{% highlight bash linenos %}
@@ -60,7 +55,6 @@ $ node PutObject.js <bucketName> <keyName> <filePath> [storageClass]
6055
{% endhighlight %}
6156
</div>
6257

63-
如果浏览器控制台出现了CORS 相关的错误信息,请检查是否正确完成跨域配置。
6458

6559
### 注意
6660
* PutObject 目前仅支持 5GB 大小文件,如果需要上传大于 5GB 的文件,请采用分片上传的 API

0 commit comments

Comments
 (0)