本SDK提供PutObject
类用于指定存储类型上传操作,大文件(5G以上)请使用分片上传,具体内容详见 官方文档 。
参数说明
Bucket
: 文件上传后所在的存储空间Key
: 文件上传后在存储空间里的名称SourceFile
: 待上传的文件全路径StorageClass
: S3 存储类型
存储类型转换规则
US3存储类型 | S3存储类型 | US3对应S3默认存储类型 |
---|---|---|
STANDARD | STANDARD STANDARD_IA |
STANDARD |
IA | ONEZONE_IA INTELLIGENT_TIERING REDUCED_REDUNDANCY |
ONEZONE_IA |
ARCHIVE | GLACIER DEEP_ARCHIVE |
GLACIER |
示例
执行该示例前请确保配置文件的正确性
以下代码段需要在上下文中运行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 初始化 S3 客户端
try {
$s3Client = new S3Client([
'endpoint' => $endpoint,
'region' => $region,
'signature_version' => $signatureVersion,
'force_path_style' => $forcePathStyle,
'credentials' => [
'key' => $accessKey,
'secret' => $secretKey,
],
]);
echo "Client initialized successfully!" . PHP_EOL;
} catch (InvalidArgumentException $e) {
die("Failed to initialize Client: " . $e->getMessage());
}
try {
$params = [
'Bucket' => $bucketName,
'Key' => $keyName,
'SourceFile' => $filePath,
];
if ($storageClass) {
$params['StorageClass'] = $storageClass;
}
// 上传文件
$result = $s3Client->putObject($params);
echo "File uploaded successfully: " . $result['ObjectURL'] . PHP_EOL;
} catch (AwsException $e) {
echo "Error uploading file: " . $e->getMessage() . PHP_EOL;
}