开发者社区> 问答> 正文

php sdk修改文件元信息

已解决

展开
收起
2018-02-07 19:16:03 689 0
1 条回答
写回答
取消 提交回答
  • 采纳回答

    详细解答可以参考官方帮助文档

    您可以通过一系列的接口管理存储空间(Bucket)下的文件(Object)。管理文件的完整代码请参见:GitHub

    判断文件是否存在

    以下代码用于判断指定的文件是否存在:

    1. <?php
    2. if (is_file(__DIR__ . '/../autoload.php')) {
    3. require_once __DIR__ . '/../autoload.php';
    4. }
    5. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    6. require_once __DIR__ . '/../vendor/autoload.php';
    7. }
    8. use OSS\OssClient;
    9. use OSS\Core\OssException;
    10. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
    11. $accessKeyId = "<yourAccessKeyId>";
    12. $accessKeySecret = "<yourAccessKeySecret>";
    13. // Endpoint以杭州为例,其它Region请按实际情况填写。
    14. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    15. $bucket= "<yourBucketName>";
    16. $object = "<yourObjectName>";
    17. try{
    18. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    19. $exist = $ossClient->doesObjectExist($bucket, $object);
    20. } catch(OssException $e) {
    21. printf(__FUNCTION__ . ": FAILED\n");
    22. printf($e->getMessage() . "\n");
    23. return;
    24. }
    25. print(__FUNCTION__ . ": OK" . "\n");
    26. var_dump($exist);

    文件访问权限

    文件的访问权限(ACL)有以下四种:

    访问权限 描述 访问权限值
    继承Bucket 文件遵循存储空间的访问权限。 default
    私有 文件的拥有者和授权用户有该文件的读写权限,其他用户没有权限操作该文件。 private
    公共读 文件的拥有者和授权用户有该文件的读写权限,其他用户只有文件的读权限。请谨慎使用该权限。 public-read
    公共读写 所有用户都有该文件的读写权限。请谨慎使用该权限。 public-read-write

    文件的访问权限优先级高于存储空间的访问权限。例如存储空间的访问权限是私有,而文件的访问权限是公共读写,则所有用户都有该文件的读写权限。如果某个文件没有设置过访问权限,则遵循存储空间的访问权限。

    设置文件访问权限

    以下代码用于设置指定文件的访问权限:

    1. <?php
    2. if (is_file(__DIR__ . '/../autoload.php')) {
    3. require_once __DIR__ . '/../autoload.php';
    4. }
    5. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    6. require_once __DIR__ . '/../vendor/autoload.php';
    7. }
    8. use OSS\OssClient;
    9. use OSS\Core\OssException;
    10. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
    11. $accessKeyId = "<yourAccessKeyId>";
    12. $accessKeySecret = "<yourAccessKeySecret>";
    13. // Endpoint以杭州为例,其它Region请按实际情况填写。
    14. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    15. $bucket= "<yourBucketName>";
    16. $object = "<yourObjectName>";
    17. // 设置文件的访问权限为公共读,默认为继承Bucket。
    18. $acl = "public-read";
    19. try {
    20. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    21. $ossClient->putObjectAcl($bucket, $object, $acl);
    22. } catch (OssException $e) {
    23. printf(__FUNCTION__ . ": FAILED\n");
    24. printf($e->getMessage() . "\n");
    25. return;
    26. }
    27. print(__FUNCTION__ . ": OK" . "\n");

    获取文件访问权限

    以下代码用于获取指定文件的访问权限:

    1. <?php
    2. if (is_file(__DIR__ . '/../autoload.php')) {
    3. require_once __DIR__ . '/../autoload.php';
    4. }
    5. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    6. require_once __DIR__ . '/../vendor/autoload.php';
    7. }
    8. use OSS\OssClient;
    9. use OSS\Core\OssException;
    10. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
    11. $accessKeyId = "<yourAccessKeyId>";
    12. $accessKeySecret = "<yourAccessKeySecret>";
    13. // Endpoint以杭州为例,其它Region请按实际情况填写。
    14. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    15. $bucket= "<yourBucketName>";
    16. $object = "<yourObjectName>";
    17. try {
    18. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    19. $objectAcl = $ossClient->getObjectAcl($bucket, $object);
    20. } catch (OssException $e) {
    21. printf(__FUNCTION__ . ": FAILED\n");
    22. printf($e->getMessage() . "\n");
    23. return;
    24. }
    25. print(__FUNCTION__ . ": OK" . "\n");
    26. var_dump($objectAcl);

    文件元信息

    文件元信息(Object Meta)详情请参见开发指南中的文件元信息

    设置文件元信息

    以下代码用于设置文件元信息:

    1. <?php
    2. if (is_file(__DIR__ . '/../autoload.php')) {
    3. require_once __DIR__ . '/../autoload.php';
    4. }
    5. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    6. require_once __DIR__ . '/../vendor/autoload.php';
    7. }
    8. use OSS\OssClient;
    9. use OSS\Core\OssException;
    10. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
    11. $accessKeyId = "<yourAccessKeyId>";
    12. $accessKeySecret = "<yourAccessKeySecret>";
    13. // Endpoint以杭州为例,其它Region请按实际情况填写。
    14. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    15. $bucket= "<yourBucketName>";
    16. $object = "<yourObjectName>";
    17. $content = file_get_contents(__FILE__);
    18. $options = array(
    19. OssClient::OSS_HEADERS => array(
    20. 'Expires' => '2012-10-01 08:00:00',
    21. 'Content-Disposition' => 'attachment; filename="xxxxxx"',
    22. 'x-oss-meta-self-define-title' => 'user define meta info',
    23. ));
    24. try{
    25. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    26. $ossClient->putObject($bucket, $object, $content, $options);
    27. } catch(OssException $e) {
    28. printf(__FUNCTION__ . ": FAILED\n");
    29. printf($e->getMessage() . "\n");
    30. return;
    31. }
    32. print(__FUNCTION__ . ": OK" . "\n");

    修改文件元信息

    以下代码用于修改文件元信息:

    1. <?php
    2. if (is_file(__DIR__ . '/../autoload.php')) {
    3. require_once __DIR__ . '/../autoload.php';
    4. }
    5. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    6. require_once __DIR__ . '/../vendor/autoload.php';
    7. }
    8. use OSS\OssClient;
    9. use OSS\Core\OssException;
    10. /// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
    11. $accessKeyId = "<yourAccessKeyId>";
    12. $accessKeySecret = "<yourAccessKeySecret>";
    13. // Endpoint以杭州为例,其它Region请按实际情况填写。
    14. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    15. $fromBucket = "<yourFromBucketName>";
    16. $fromObject = "<yourFromObjectName>";
    17. $toBucket = "<yourToBucketName>";
    18. $toObject = "<yourToObjectName>";
    19. $copyOptions = array(
    20. OssClient::OSS_HEADERS => array(
    21. 'Expires' => '2018-10-01 08:00:00',
    22. 'Content-Disposition' => 'attachment; filename="xxxxxx"',
    23. 'x-oss-meta-location' => 'location',
    24. ),
    25. );
    26. try{
    27. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    28. $ossClient->copyObject($fromBucket, $fromObject, $toBucket, $toObject, $copyOptions);
    29. } catch(OssException $e) {
    30. printf(__FUNCTION__ . ": FAILED\n");
    31. printf($e->getMessage() . "\n");
    32. return;
    33. }
    34. print(__FUNCTION__ . ": OK" . "\n");

    获取文件元信息

    以下代码用于获取文件元信息:

    1. <?php
    2. if (is_file(__DIR__ . '/../autoload.php')) {
    3. require_once __DIR__ . '/../autoload.php';
    4. }
    5. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    6. require_once __DIR__ . '/../vendor/autoload.php';
    7. }
    8. use OSS\OssClient;
    9. use OSS\Core\OssException;
    10. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
    11. $accessKeyId = "<yourAccessKeyId>";
    12. $accessKeySecret = "<yourAccessKeySecret>";
    13. // Endpoint以杭州为例,其它Region请按实际情况填写。
    14. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    15. $bucket= "<yourBucketName>";
    16. $object = "<yourObjectName>";
    17. try {
    18. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    19. $objectMeta = $ossClient->getObjectMeta($bucket, $object);
    20. } catch (OssException $e) {
    21. printf(__FUNCTION__ . ": FAILED\n");
    22. printf($e->getMessage() . "\n");
    23. return;
    24. }
    25. print(__FUNCTION__ . ": OK" . "\n");
    26. if (isset($objectMeta[strtolower('Content-Disposition')]) &&
    27. 'attachment; filename="xxxxxx"' === $objectMeta[strtolower('Content-Disposition')]
    28. ) {
    29. print(__FUNCTION__ . ": ObjectMeta checked OK" . "\n");
    30. } else {
    31. print(__FUNCTION__ . ": ObjectMeta checked FAILED" . "\n");
    32. }

    列举文件

    列举所有文件

    以下代码用于列举指定存储空间下的所有文件:

    1. <?php
    2. if (is_file(__DIR__ . '/../autoload.php')) {
    3. require_once __DIR__ . '/../autoload.php';
    4. }
    5. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    6. require_once __DIR__ . '/../vendor/autoload.php';
    7. }
    8. use OSS\OssClient;
    9. use OSS\Core\OssException;
    10. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
    11. $accessKeyId = "<yourAccessKeyId>";
    12. $accessKeySecret = "<yourAccessKeySecret>";
    13. // Endpoint以杭州为例,其它Region请按实际情况填写。
    14. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    15. $bucket= "<yourBucketName>";
    16. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    17. while (true) {
    18. try {
    19. $listObjectInfo = $ossClient->listObjects($bucket, $options);
    20. } catch (OssException $e) {
    21. printf(__FUNCTION__ . ": FAILED\n");
    22. printf($e->getMessage() . "\n");
    23. return;
    24. }
    25. // 得到nextMarker,从上一次listObjects读到的最后一个文件的下一个文件开始继续获取文件列表。
    26. $nextMarker = $listObjectInfo->getNextMarker();
    27. $listObject = $listObjectInfo->getObjectList();
    28. $listPrefix = $listObjectInfo->getPrefixList();
    29. if (!empty($listObject)) {
    30. print("objectList:\n");
    31. foreach ($listObject as $objectInfo) {
    32. print($objectInfo->getKey() . "\n");
    33. }
    34. }
    35. if (!empty($listPrefix)) {
    36. print("prefixList: \n");
    37. foreach ($listPrefix as $prefixInfo) {
    38. print($prefixInfo->getPrefix() . "\n");
    39. }
    40. }
    41. if ($nextMarker === '') {
    42. break;
    43. }
    44. }

    列举指定条件的文件

    以下代码用于列举指定条件的文件:

    1. <?php
    2. if (is_file(__DIR__ . '/../autoload.php')) {
    3. require_once __DIR__ . '/../autoload.php';
    4. }
    5. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    6. require_once __DIR__ . '/../vendor/autoload.php';
    7. }
    8. use OSS\OssClient;
    9. use OSS\Core\OssException;
    10. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
    11. $accessKeyId = "<yourAccessKeyId>";
    12. $accessKeySecret = "<yourAccessKeySecret>";
    13. // Endpoint以杭州为例,其它Region请按实际情况填写。
    14. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    15. $bucket= "<yourBucketName>";
    16. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    17. $prefix = 'dir/';
    18. $delimiter = '/';
    19. $nextMarker = '';
    20. $maxkeys = 10;
    21. $options = array(
    22. 'delimiter' => $delimiter,
    23. 'prefix' => $prefix,
    24. 'max-keys' => $maxkeys,
    25. 'marker' => $nextMarker,
    26. );
    27. try {
    28. $listObjectInfo = $ossClient->listObjects($bucket, $options);
    29. } catch (OssException $e) {
    30. printf(__FUNCTION__ . ": FAILED\n");
    31. printf($e->getMessage() . "\n");
    32. return;
    33. }
    34. print(__FUNCTION__ . ": OK" . "\n");
    35. $objectList = $listObjectInfo->getObjectList(); // object list
    36. $prefixList = $listObjectInfo->getPrefixList(); // directory list
    37. if (!empty($objectList)) {
    38. print("objectList:\n");
    39. foreach ($objectList as $objectInfo) {
    40. print($objectInfo->getKey() . "\n");
    41. }
    42. }
    43. if (!empty($prefixList)) {
    44. print("prefixList: \n");
    45. foreach ($prefixList as $prefixInfo) {
    46. print($prefixInfo->getPrefix() . "\n");
    47. }
    48. }

    上述例子中的$options包含的参数如下:

    参数 说明 是否必需
    delimiter 对文件名称进行分组的一个字符。CommonPrefixes是以delimiter结尾,并有共同前缀的文件集合。
    prefix 本次查询结果的前缀。
    max-keys 列举文件的最大个数。默认为100,最大值为1000。
    marker 标明本次列举文件的起点。

    删除文件

    删除单个文件

    以下代码用于删除单个文件:

    1. <?php
    2. if (is_file(__DIR__ . '/../autoload.php')) {
    3. require_once __DIR__ . '/../autoload.php';
    4. }
    5. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    6. require_once __DIR__ . '/../vendor/autoload.php';
    7. }
    8. use OSS\OssClient;
    9. use OSS\Core\OssException;
    10. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
    11. $accessKeyId = "<yourAccessKeyId>";
    12. $accessKeySecret = "<yourAccessKeySecret>";
    13. // Endpoint以杭州为例,其它Region请按实际情况填写。
    14. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    15. $bucket= "<yourBucketName>";
    16. $object = "<yourObjectName>";
    17. try{
    18. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    19. $ossClient->deleteObject($bucket, $object);
    20. } catch(OssException $e) {
    21. printf(__FUNCTION__ . ": FAILED\n");
    22. printf($e->getMessage() . "\n");
    23. return;
    24. }
    25. print(__FUNCTION__ . ": OK" . "\n");

    删除多个文件

    以下代码用于批量删除文件:

    1. <?php
    2. if (is_file(__DIR__ . '/../autoload.php')) {
    3. require_once __DIR__ . '/../autoload.php';
    4. }
    5. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    6. require_once __DIR__ . '/../vendor/autoload.php';
    7. }
    8. use OSS\OssClient;
    9. use OSS\Core\OssException;
    10. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
    11. $accessKeyId = "<yourAccessKeyId>";
    12. $accessKeySecret = "<yourAccessKeySecret>";
    13. // Endpoint以杭州为例,其它Region请按实际情况填写。
    14. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    15. $bucket= "<yourBucketName>";
    16. $objects = array();
    17. $objects[] = "<yourObjectName1>";
    18. $objects[] = "<yourObjectName2>";
    19. try{
    20. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    21. $ossClient->deleteObjects($bucket, $objects);
    22. } catch(OssException $e) {
    23. printf(__FUNCTION__ . ": FAILED\n");
    24. printf($e->getMessage() . "\n");
    25. return;
    26. }
    27. print(__FUNCTION__ . ": OK" . "\n");

    拷贝文件

    简单拷贝

    以下代码用于拷贝小于1GB的文件:

    1. <?php
    2. if (is_file(__DIR__ . '/../autoload.php')) {
    3. require_once __DIR__ . '/../autoload.php';
    4. }
    5. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    6. require_once __DIR__ . '/../vendor/autoload.php';
    7. }
    8. use OSS\OssClient;
    9. use OSS\Core\OssException;
    10. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
    11. $accessKeyId = "<yourAccessKeyId>";
    12. $accessKeySecret = "<yourAccessKeySecret>";
    13. // Endpoint以杭州为例,其它Region请按实际情况填写。
    14. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    15. $from_bucket = "<yourFromBucketName>";
    16. $from_object = "<yourFromObjectName>";
    17. $to_bucket = $bucket;
    18. $to_object = $from_object . '.copy';
    19. try{
    20. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    21. $ossClient->copyObject($from_bucket, $from_object, $to_bucket, $to_object);
    22. } catch(OssException $e) {
    23. printf(__FUNCTION__ . ": FAILED\n");
    24. printf($e->getMessage() . "\n");
    25. return;
    26. }
    27. print(__FUNCTION__ . ": OK" . "\n");
    28. `

    拷贝大文件

    对于大于1GB的文件,需要使用分片拷贝。分片拷贝分为三步:

    1. 通过$ossClient->initiateMultipartUpload初始化分片拷贝任务。
    2. 通过$ossClient->uploadPartCopy进行分片拷贝。除最后一个分片外,其它分片都要大于100KB。
    3. 通过$ossClient->completeMultipartUpload提交分片拷贝任务。

    以下代码用于分片拷贝:

    1. <?php
    2. if (is_file(__DIR__ . '/../autoload.php')) {
    3. require_once __DIR__ . '/../autoload.php';
    4. }
    5. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    6. require_once __DIR__ . '/../vendor/autoload.php';
    7. }
    8. use OSS\OssClient;
    9. use OSS\Core\OssException;
    10. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
    11. $accessKeyId = "<yourAccessKeyId>";
    12. $accessKeySecret = "<yourAccessKeySecret>";
    13. // Endpoint以杭州为例,其它Region请按实际情况填写。
    14. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    15. $src_bucket = "<yourSourceBucketName>";
    16. $src_object = "<yourSourceObjectName>";
    17. $dst_bucket = "<yourDestinationBucketName>";
    18. $dst_object = "<yourDestinationObjectName>";
    19. try{
    20. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    21. // 初始化分片。
    22. $upload_id = $ossClient->initiateMultipartUpload($dst_bucket, $dst_object);
    23. $copyId = 1;
    24. // 逐个分片拷贝。
    25. $eTag = $ossClient->uploadPartCopy( $src_bucket, $src_object, $dst_bucket, $dst_object,$copyId, $upload_id);
    26. $upload_parts[] = array(
    27. 'PartNumber' => $copyId,
    28. 'ETag' => $eTag,
    29. );
    30. // 完成分片拷贝。
    31. $result = $ossClient->completeMultipartUpload($dst_bucket, $dst_object, $upload_id, $upload_parts);
    32. } catch(OssException $e) {
    33. printf(__FUNCTION__ . ": FAILED\n");
    34. printf($e->getMessage() . "\n");
    35. return;
    36. }
    37. print(__FUNCTION__ . ": OK" . "\n");

    解冻归档文件

    归档类型(Archive)的文件需要解冻(Restore)之后才能读取。非归档类型的文件,不要调用restoreObject方法。

    归档文件的状态变换过程如下:

    1. 归档类型的文件初始时处于冷冻状态。
    2. 提交解冻操作后,服务端执行解冻,文件处于解冻中的状态。
    3. 完成解冻后,可以读取文件。解冻状态默认持续1天,最多延长7天,之后文件又回到冷冻状态。

    以下代码用于解冻归档文件:

    1. <?php
    2. if (is_file(__DIR__ . '/../autoload.php')) {
    3. require_once __DIR__ . '/../autoload.php';
    4. }
    5. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    6. require_once __DIR__ . '/../vendor/autoload.php';
    7. }
    8. use OSS\OssClient;
    9. use OSS\Core\OssException;
    10. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
    11. $accessKeyId = "<yourAccessKeyId>";
    12. $accessKeySecret = "<yourAccessKeySecret>";
    13. // Endpoint以杭州为例,其它Region请按实际情况填写。
    14. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    15. $bucket= "<yourBucketName>";
    16. $object = "<yourObjectName>";
    17. try {
    18. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    19. $ossClient->restoreObject($bucket, $object);
    20. } catch (OssException $e) {
    21. printf(__FUNCTION__ . ": FAILED\n");
    22. printf($e->getMessage() . "\n");
    23. return;
    24. }
    25. print(__FUNCTION__ . ": OK" . "\n");

    存储类型详情请参见存储类型

    符号链接

    创建符号链接

    符号链接是一种特殊的文件,它指向某个具体的文件,类似于Windows上使用的快捷方式。符号链接支持自定义元信息。

    以下代码用于创建符号链接:

    1. <?php
    2. if (is_file(__DIR__ . '/../autoload.php')) {
    3. require_once __DIR__ . '/../autoload.php';
    4. }
    5. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    6. require_once __DIR__ . '/../vendor/autoload.php';
    7. }
    8. use OSS\OssClient;
    9. use OSS\Core\OssException;
    10. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
    11. $accessKeyId = "<yourAccessKeyId>";
    12. $accessKeySecret = "<yourAccessKeySecret>";
    13. // Endpoint以杭州为例,其它Region请按实际情况填写。
    14. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    15. $bucket= "<yourBucketName>";
    16. $object = "<yourObjectName>";
    17. $symlink = "<yourSymlink>";
    18. try {
    19. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    20. $ossClient->putSymlink($bucket, $symlink, $object);
    21. } catch (OssException $e) {
    22. printf(__FUNCTION__ . ": FAILED\n");
    23. printf($e->getMessage() . "\n");
    24. return;
    25. }
    26. print(__FUNCTION__ . ": OK" . "\n");

    获取符号链接指向的文件内容

    获取符号链接要求您对该符号链接有读权限。以下代码用于获取符号链接指向的文件内容:

    1. <?php
    2. if (is_file(__DIR__ . '/../autoload.php')) {
    3. require_once __DIR__ . '/../autoload.php';
    4. }
    5. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    6. require_once __DIR__ . '/../vendor/autoload.php';
    7. }
    8. use OSS\OssClient;
    9. use OSS\Core\OssException;
    10. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
    11. $accessKeyId = "<yourAccessKeyId>";
    12. $accessKeySecret = "<yourAccessKeySecret>";
    13. // Endpoint以杭州为例,其它Region请按实际情况填写。
    14. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    15. $bucket= "<yourBucketName>";
    16. $symlink = "<yourSymlink>";
    17. try {
    18. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    19. $Symlinks = $ossClient->getSymlink($bucket, $symlink);
    20. } catch (OssException $e) {
    21. printf(__FUNCTION__ . ": FAILED\n");
    22. printf($e->getMessage() . "\n");
    23. return;
    24. }
    25. print(__FUNCTION__ . ": OK" . "\n");
    26. var_dump($Symlinks);

    开启MD5校验

    MD5校验用于确保数据传输的完整性。使用MD5校验时,性能会有所损失。上传文件时默认关闭MD5校验。

    以下代码用于上传文件时开启MD5校验:

    1. <?php
    2. if (is_file(__DIR__ . '/../autoload.php')) {
    3. require_once __DIR__ . '/../autoload.php';
    4. }
    5. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    6. require_once __DIR__ . '/../vendor/autoload.php';
    7. }
    8. use OSS\OssClient;
    9. use OSS\Core\OssException;
    10. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
    11. $accessKeyId = "<yourAccessKeyId>";
    12. $accessKeySecret = "<yourAccessKeySecret>";
    13. // Endpoint以杭州为例,其它Region请按实际情况填写。
    14. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    15. $bucket= "<yourBucketName>";
    16. $object = "<yourObjectName>";
    17. $options = array(OssClient::OSS_CHECK_MD5 => true);
    18. try{
    19. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    20. $ossClient->uploadFile($bucket, $object, __FILE__, $options);
    21. } catch(OssException $e) {
    22. printf(__FUNCTION__ . ": FAILED\n");
    23. printf($e->getMessage() . "\n");
    24. return;
    25. }
    26. print(__FUNCTION__ . ": OK" . "\n");

    putObject、uploadFile、appendObject、appendFile、multiuploadFile方法支持开启MD5校验。

    2018-02-13 11:12:04
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
阿里云栖开发者沙龙PHP技术专场-聊聊服务稳定性保障这些事-信海龙 立即下载
阿里云栖开发者沙龙PHP技术专场-深入浅出网络编程与swoole内核-吴镇宇 立即下载
一个跨平台的云服务SDK需要什么 立即下载