检查云文件中是否存在对象(PHP API)

时间:2023-01-06 10:28:51

I've just started working with the PHP API for Rackspace Cloud Files. So far so good-- but I am using it as sort of a poor man's memcache, storing key/value pairs of serialized data.

我刚刚开始使用用于Rackspace云文件的PHP API。到目前为止还不错——但我把它用作穷人的记忆缓存,存储序列化数据的键/值对。

My app attempts to grab the existing cached object by its key ('name' in the API language) using something like this:

我的应用程序试图通过它的键(API语言中的“name”)获取现有的缓存对象,使用如下方法:

$obj = $this->container->get_object($key);

The problem is, if the object doesn't exist, the API throws a fatal error rather than simply returning false. The "right" way to do this by the API would probably be to do a

问题是,如果对象不存在,API会抛出一个致命错误,而不是简单地返回false。API实现此目的的“正确”方法可能是执行a

$objs = $this->container->list_objects();

and then check for my $key value in that list. However, this seems way more time/CPU intensive than just returning false from the get_object request.

然后检查列表中的$key值。然而,这似乎比从get_object请求返回false要花费更多的时间/CPU时间。

Is there a way to do a "search for object" or "check if object exists" in Cloud Files?

是否有一种方法可以在云文件中进行“搜索对象”或“检查对象是否存在”?

Thanks

谢谢

5 个解决方案

#1


4  

I sent them a pull request and hope it'll get included.

我给他们发了一个拉取请求,希望能包括在内。

https://github.com/rackspace/php-cloudfiles/pull/35

https://github.com/rackspace/php-cloudfiles/pull/35

My pull-request includes an example, for you it would be similar to this:

我的下拉请求包括一个例子,对你来说它将类似于:

$object = new CF_Object($this->container, 'key');
if ($object->exists() === false) {
    echo "The object '{$object->name}' does not exist.";
}

#2


1  

I have more general way to check if object exists:

我有更一般的方法来检查对象是否存在:

    try {
        $this->_container->get_object($path);
        $booExists = true;
    } catch (Exception $e) {
        $booExists = false;
    }

#3


0  

If you dump the $object, you'll see that content_length is zero. Or, last modified will be a zero length string.

如果转储$对象,则会看到content_length为零。或者,最后修改的将是一个零长度的字符串。

Example:

例子:

$object = new CF_Object($container, 'thisdocaintthere.pdf');
print_r($object->content_length);

There is also, deep in the dumped parent object, a 404 that will return, but it's private, so you'd need to some hackin' to get at it.

在被转储的父对象的深处,还有一个404将返回,但它是私有的,所以需要一些黑客才能找到它。

To see this, do the following:

要了解这一点,请执行以下操作:

$object = new CF_Object($container, 'thisdocaintthere.pdf');
print_r($object->container->cfs_http);

You'll see inside that object a response_status that is 404

您将在该对象中看到一个response_status,即404

[response_status:CF_Http:private] => 404

#4


0  

I know I'm a little late to the party, but hopefully this will help someone in the future: you can use the objectExists() method to test if an object is available.

我知道我参加聚会有点晚了,但希望这对将来的某人有所帮助:您可以使用objectexist()方法来测试对象是否可用。

public static function getObject($container, $filename, $expirationTime = false)
{
    if ($container->objectExists($filename)) {

        $object = $container->getPartialObject($filename);

        // return a private, temporary url
        if ($expirationTime) {
            return $object->getTemporaryUrl($expirationTime, 'GET');
        }

        // return a public url
        return $object->getPublicUrl();
    }

    // object does not exist
    return '';
}

Use like...

使用像…

// public CDN file
$photo = self::getObject($container, 'myPublicfile.jpg');

// private file; temporary link expires after 60 seconds
$photo = self::getObject($container, 'myPrivatefile.jpg', 60);

#5


0  

If you do not want to import opencloud to perform this check you can use the following:

如果您不想导入opencloud来执行此检查,您可以使用以下方法:

$url = 'YOUR CDN URL';
$code = FALSE;
$options['http'] = array(
    'method' => "HEAD",
    'ignore_errors' => 1,
    'max_redirects' => 0
);
$body = file_get_contents($url, NULL, stream_context_create($options));
sscanf($http_response_header[0], 'HTTP/%*d.%*d %d', $code);


if($code!='200') {
    echo 'failed';
} else {
    echo 'exists';
}

#1


4  

I sent them a pull request and hope it'll get included.

我给他们发了一个拉取请求,希望能包括在内。

https://github.com/rackspace/php-cloudfiles/pull/35

https://github.com/rackspace/php-cloudfiles/pull/35

My pull-request includes an example, for you it would be similar to this:

我的下拉请求包括一个例子,对你来说它将类似于:

$object = new CF_Object($this->container, 'key');
if ($object->exists() === false) {
    echo "The object '{$object->name}' does not exist.";
}

#2


1  

I have more general way to check if object exists:

我有更一般的方法来检查对象是否存在:

    try {
        $this->_container->get_object($path);
        $booExists = true;
    } catch (Exception $e) {
        $booExists = false;
    }

#3


0  

If you dump the $object, you'll see that content_length is zero. Or, last modified will be a zero length string.

如果转储$对象,则会看到content_length为零。或者,最后修改的将是一个零长度的字符串。

Example:

例子:

$object = new CF_Object($container, 'thisdocaintthere.pdf');
print_r($object->content_length);

There is also, deep in the dumped parent object, a 404 that will return, but it's private, so you'd need to some hackin' to get at it.

在被转储的父对象的深处,还有一个404将返回,但它是私有的,所以需要一些黑客才能找到它。

To see this, do the following:

要了解这一点,请执行以下操作:

$object = new CF_Object($container, 'thisdocaintthere.pdf');
print_r($object->container->cfs_http);

You'll see inside that object a response_status that is 404

您将在该对象中看到一个response_status,即404

[response_status:CF_Http:private] => 404

#4


0  

I know I'm a little late to the party, but hopefully this will help someone in the future: you can use the objectExists() method to test if an object is available.

我知道我参加聚会有点晚了,但希望这对将来的某人有所帮助:您可以使用objectexist()方法来测试对象是否可用。

public static function getObject($container, $filename, $expirationTime = false)
{
    if ($container->objectExists($filename)) {

        $object = $container->getPartialObject($filename);

        // return a private, temporary url
        if ($expirationTime) {
            return $object->getTemporaryUrl($expirationTime, 'GET');
        }

        // return a public url
        return $object->getPublicUrl();
    }

    // object does not exist
    return '';
}

Use like...

使用像…

// public CDN file
$photo = self::getObject($container, 'myPublicfile.jpg');

// private file; temporary link expires after 60 seconds
$photo = self::getObject($container, 'myPrivatefile.jpg', 60);

#5


0  

If you do not want to import opencloud to perform this check you can use the following:

如果您不想导入opencloud来执行此检查,您可以使用以下方法:

$url = 'YOUR CDN URL';
$code = FALSE;
$options['http'] = array(
    'method' => "HEAD",
    'ignore_errors' => 1,
    'max_redirects' => 0
);
$body = file_get_contents($url, NULL, stream_context_create($options));
sscanf($http_response_header[0], 'HTTP/%*d.%*d %d', $code);


if($code!='200') {
    echo 'failed';
} else {
    echo 'exists';
}