简体   繁体   English

如何使用PHP创建临时URL以将文件上传到RackSpace Cloud Files?

[英]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云文档中 ,您可以将“ GET”更改为“ PUT”,以达到我认为可以放置文件的目的,而不是获取文件,问题是该文件尚不存在,而且显然是唯一的创建文件的方法是首先上传它? PHP SDK Container PHP SDK容器

In Amazon s3 I can do the following to get what I want: 在Amazon s3中,我可以执行以下操作以获得所需的信息:

$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: 然后,我可以按照自己喜欢的任何方式写入presignedUrl,就像使用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. 因此,基本上我想做的是像从Amazon s3一样,从RackSpace获取上传URL并将其写入。

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. 我需要这样做,因为我的API仅提供下载和上传链接,因此没有流量直接通过它。 I can't have it saving the files to my API server then upload it to cloud. 我无法将文件保存到我的API服务器,然后将其上传到云。

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. 是的,您可以模拟文件上传而无需实际将内容上传到API-您要做的就是确定文件名。 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. getObject方法返回一个空对象,您要做的就是在该对象上设置远程名称。 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. 创建临时URL时,它将使用您刚刚设置的名称,并提供一个临时URL供您使用-不管对象是否位于远程。 The temp URL can then be used to create the object. 然后可以使用临时URL创建对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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