简体   繁体   中英

How to create Temporary URL to upload file to RackSpace Cloud Files using PHP?

I have this code to get files from rackspace cloud files:

$username = getenv('RS_USERNAME');
$apikey = getenv('RS_APIKEY');
$containerName = 'imagenesfc';
$region = 'DFW';
$client = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, array(
    'username' => $username,
    'apiKey'   => $apikey,
));
$filename = "myfile.ext";

$service = $client->objectStoreService(null, $region);
$container = $service->getContainer($containerName);

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

$account = $service->getAccount();
$account->setTempUrlSecret();
$tempUrl = $object->getTemporaryUrl(15, 'GET');

In the open php cloud documentation says you can change 'GET' to 'PUT' to what I imagine is being able to put a file, instead of getting it, the problem is that the file doesn't exist yet, and apparently the only way to create a file is uploading it first? PHP SDK Container

In Amazon s3 I can do the following to get what I want:

$keyname = "myfile.ext";

$arr = array(
    'Bucket' => 'bucket',
    'Key'    => $keyname
    );

$cmd = $s3Client->getCommand('PutObject', $arr);

$request = $s3Client->createPresignedRequest($cmd, '+10 minutes');
$presignedUrl = (string) $request->getUri();

Then I can write to the presignedUrl anyway I prefer, like with Java:

url = new URL(jObj.get("presignedUrl").toString().replace("\\", ""));
connection=(HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
out = new DataOutputStream(connection.getOutputStream());
...(write,flush,close)

So, basically what I want to do, is get the upload URL from RackSpace and write to it like I do with Amazon s3.

Is it possible? And if it is, how can you do it?

I need to do it this way because my API will provide only download and upload links, so no traffic goes directly through it. I can't have it saving the files to my API server then upload it to cloud.

Yes, you can simulate a file upload without actually uploading content to the API - all you need to do is determine what the filename will be. The code you will need is:

$object = $container->getObject();
$object->setName('blah.txt');

$account = $service->getAccount();
$account->setTempUrlSecret();

echo $object->getTemporaryUrl(100, 'PUT');

The getObject method returns an empty object, and all you're doing is setting the remote name on that object. When a temp URL is created, it uses the name you just set and presents a temporary URL for you to use - regardless if the object exists remotely. The temp URL can then be used to create the object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM