放牧代码和思想
专注自然语言处理、机器学习算法
    愛しさ 優しさ すべて投げ出してもいい

百度BCS云储存不支持操作文件夹

在多次试验之后,我不得不得出这样一个一个结论,那就是BAE的bcs不支持操作文件夹。为什么这么说呢,因为所有试图将文件夹作为对象操作的代码都会无情地得到一个error “”,比如:

$response = $baidu_bcs->delete_object($bucket, "/data/");

被error:

Body:{"Error":{"code":"2","Message":"object not exists","LogId":"901523708"}}

哪怕你的data目录下空空如也,bcs也不允许你删除它。在bcs的设计理念中,object的概念很模糊。到底什么是对象呢?文件肯定是的,那么文件夹是不是呢?我尝试用list_object_by_dir来列出根目录下的所有对象,发现/data/确实是一个对象,只不过它的is_dir属性为1,而文件的这个属性为0:

{
    "object_total":2,
    "bucket":"hankcstest",
    "start":0,
    "limit":200,
    "parent_dir":"%2F",
    "object_list":[
        {
            "version_key":"CA38A99734E885ED5D845F413480AC4F",
            "object":"/1.txt",
            "superfile":"0",
            "size":"3886",
            "parent_dir":"/",
            "is_dir":"0",
            "mdatetime":"1374052139",
            "ref_key":"",
            "content_md5":"23DA3983CB890A6C486D834F1964F9BC"
        },
        {
            "version_key":"",
            "object":"/data/",
            "superfile":"0",
            "size":"0",
            "parent_dir":"/",
            "is_dir":"1",
            "mdatetime":"1374051432",
            "ref_key":"",
            "content_md5":""
        }
    ]
}

但是,如果你试图通过delete_object来删除/data/,bcs又会告诉你这个对象不存在,真是让人迷惑。让我们从头开始再现这一悖论吧:

<?php
    echo "Welcome to Baidu Cloud<br>";
    require_once './bcs.class.php';
     
  
    $baidu_bcs = new BaiduBCS ();
    echo "BCS created<br>";
  
    $bucket = 'hankcstest';
  
    $object = "/data/index.php"; 
    $file = "index.php"; 
    $response = $baidu_bcs->create_object($bucket, $object, $file);
    echo "create file $object : ";
    echo $response->isOK () ? "OK<br>" : "NOT OK<br>"; 
    echo 'Status:' . $response->status . "<br>"; 
    echo 'Body:' . $response->body . "<br>"; 
    foreach ( $response->header as $key => $value ) {
        echo "Http Header -> $key:$value<br>";
    }
      
    $object = "/";
    $response = $baidu_bcs->list_object_by_dir($bucket, $object);
    echo "list folder $object : ";
    echo $response->isOK () ? "OK<br>" : "NOT OK<br>"; 
    echo 'Status:' . $response->status . "<br>"; 
    echo 'Body:' . $response->body . "<br>"; 
    foreach ( $response->header as $key => $value ) {
        echo "Http Header -> $key:$value<br>";
    }
  
    $object = "/data/index.php"; 
    $response = $baidu_bcs->delete_object($bucket, $object);
    echo "delete file $object : "; 
    echo $response->isOK () ? "OK<br>" : "NOT OK<br>"; 
    echo 'Status:' . $response->status . "<br>"; 
    echo 'Body:' . $response->body . "<br>"; 
    foreach ( $response->header as $key => $value ) {
        echo "Http Header -> $key:$value<br>";
    }
  
    $object = "/data/";
    $response = $baidu_bcs->list_object_by_dir($bucket, $object);
    echo "list folder $object : ";
    echo $response->isOK () ? "OK<br>" : "NOT OK<br>"; 
    echo 'Status:' . $response->status . "<br>"; 
    echo 'Body:' . $response->body . "<br>"; 
    foreach ( $response->header as $key => $value ) {
        echo "Http Header -> $key:$value<br>";
    }
  
    $object = "/data/"; 
    $response = $baidu_bcs->delete_object($bucket, $object);
    echo "delete folder $object : ";
    echo $response->isOK () ? "OK<br>" : "NOT OK<br>";  
    echo 'Status:' . $response->status . "<br>"; 
    echo 'Body:' . $response->body . "<br>"; 
    foreach ( $response->header as $key => $value ) {
        echo "Http Header -> $key:$value<br>";
    }
  
?>

我依次创建了bcs对象,上传了index.php到/data/index.php然后list了一下根目录下对象,发现了一个对象也就是我们的/data/,然后我删掉了/data/index.php,此时/data/就是空的了,然后我试图删掉/data/,失败,对象不存在,完整的输出:

Welcome to Baidu Cloud
BCS created
create file /data/index.php : OK
Status:200
Body:
Http Header -> Server:nginx/1.2.0
Http Header -> Date:Wed, 17 Jul 2013 09:16:50 GMT
Http Header -> Content-Length:0
Http Header -> Connection:keep-alive
Http Header -> Access-Control-Allow-Origin:*
Http Header -> Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS,HEAD
Http Header -> x-bs-version:BA45FE1D2198269804DCC07F015996F4
Http Header -> ETag:64b217bb1f3aa429f3226d9c2edb37f4
Http Header -> Content-MD5:64b217bb1f3aa429f3226d9c2edb37f4
Http Header -> x-bs-request-id:MTAuMzYuMTAwLjE2OjgwODA6MTEzNDYyOTgwOToxNy9KdWwvMjAxMyAxNzoxNjo1MCA=
Http Header -> x-bs-file-size:1107
Http Header -> x-bs-client-ip:MjIwLjE4MS41MS4xNDI=
Http Header -> _info:Array
list folder / : OK
Status:200
Body:{"object_total":1,"bucket":"hankcstest","start":0,"limit":200,"parent_dir":"%2F","object_list":[{"version_key":"","object":"\/data\/","superfile":"0","size":"0","parent_dir":"\/","is_dir":"1","mdatetime":"1374051432","ref_key":"","content_md5":""}]}
Http Header -> Server:nginx/1.2.0
Http Header -> Date:Wed, 17 Jul 2013 09:16:50 GMT
Http Header -> Content-Type:application/json
Http Header -> Transfer-Encoding:chunked
Http Header -> Connection:keep-alive
Http Header -> Access-Control-Allow-Origin:*
Http Header -> Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS,HEAD
Http Header -> x-bs-request-id:Y3EwMS1iYWUtb2ZmdWkwLTkuY3EwMS5iYWlkdS5jb206ODA6Mzc1NzQ2MzM5NTU=
Http Header -> x-bs-client-ip:MjIwLjE4MS41MS4xMzk=
Http Header -> _info:Array
delete file /data/index.php : OK
Status:200
Body:
Http Header -> Server:nginx/1.2.0
Http Header -> Date:Wed, 17 Jul 2013 09:16:50 GMT
Http Header -> Content-Length:0
Http Header -> Connection:keep-alive
Http Header -> Access-Control-Allow-Origin:*
Http Header -> Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS,HEAD
Http Header -> x-bs-client-ip:MjIwLjE4MS41MS4xNDE=
Http Header -> _info:Array
list folder /data/ : OK
Status:200
Body:{"object_total":0,"bucket":"hankcstest","start":0,"limit":200,"parent_dir":"%2Fdata%2F","object_list":[]}
Http Header -> Server:nginx/1.2.0
Http Header -> Date:Wed, 17 Jul 2013 09:16:50 GMT
Http Header -> Content-Type:application/json
Http Header -> Transfer-Encoding:chunked
Http Header -> Connection:keep-alive
Http Header -> Access-Control-Allow-Origin:*
Http Header -> Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS,HEAD
Http Header -> x-bs-request-id:bTEtYmFlLW9mZnVpMC03Lm0xLmJhaWR1LmNvbTo4MDozODE0NTM2MDgxMQ==
Http Header -> x-bs-client-ip:MjIwLjE4MS41MS4xNDk=
Http Header -> _info:Array
delete folder /data/ : NOT OK
Status:404
Body:{"Error":{"code":"2","Message":"object not exists","LogId":"2443104357"}}
Http Header -> Server:nginx/1.2.0
Http Header -> Date:Wed, 17 Jul 2013 09:16:50 GMT
Http Header -> Content-Type:application/json
Http Header -> Content-Length:73
Http Header -> Connection:keep-alive
Http Header -> Access-Control-Allow-Origin:*
Http Header -> Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS,HEAD
Http Header -> x-bs-request-id:MTAuMzYuMTM1LjM2OjgwODA6MjQ0MzEwNDM1NzoxNy9KdWwvMjAxMyAxNzoxNjo1MCA=
Http Header -> _info:Array

你可能会觉得奇怪,既然文件夹删不掉,那么delete_bucket不就也失去了存在的意义吗?一个永远清空不料的bucket如何删得掉?BAE怪人有怪招,使用一句delete_object($bucket, "/")之后整个bucket就消失了,你说怪不怪?

我在想这是不是BAE的设计失误,希望某一天BAE能够重新设计它的bcs libs。

知识共享许可协议 知识共享署名-非商业性使用-相同方式共享码农场 » 百度BCS云储存不支持操作文件夹

评论 6

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #2

    把"/data/"换成"/data",不知道可不可以删掉。。。

    神情话意11年前 (2013-11-24)回复
    • bcs.class.php中的146-148行、278-286行

      神情话意11年前 (2013-11-24)回复
    • 应该不行的吧,假如根目录下有一个文件名叫data,那么delete_object($bucket, "/data")到底是删文件还是删目录呢?

      hankcs11年前 (2013-11-24)回复
  2. #1

    这个可以在版本管理,设置里面开通……

    何小点点_Hoxiudimdim11年前 (2013-11-09)回复
    • 你指的是NFS吧?我说的是bcs……

      hankcs11年前 (2013-11-09)回复

我的作品

HanLP自然语言处理包《自然语言处理入门》