Sina App Engine(SAE)入門教程(7)- Storage使用

來源:互聯網
上載者:User

標籤:

參考閱讀
  • sae storage api 文檔
  • Storage 說明文檔
  • Storage 大檔案上傳說明
storage是什嗎?

因為sae禁用了代碼環境的本地讀寫,但是在網站啟動並執行過程中,必定會出現檔案的讀寫,附件儲存問題,此時就該存取storage出場了,storage代替了常規的upload目錄~

 

怎麼在sae使用storage?

見下面的圖示:

輸入domain的名字,其他的建議不需要填寫,特別是防盜鏈那塊,對於不熟悉的人而言,設定可能導致storage內的資源無法訪問,可以等徹底熟悉了storage的工作原理後再設定不遲:)我此處建立一個名字叫lazy的domain用於教學。

到此你就已經建立完了storage,

你可以在sae的管理面板就是上面圖片中點擊“管理” 管理storage中的檔案,進行上傳,批量上傳,刪除等一系列操作。

下文主要介紹從代碼的角度使用storage,舉一個具體的執行個體說明如何將使用者上傳的圖片寫到storage中。

初實storage

我們可以建立如下的指令碼看看現在的storage

<?php$stor = new SaeStorage();$domain = ‘lazy‘;//我剛建立的domain的名稱$filename = ‘remote_file.txt‘;$content = ‘hello lazy‘;$stor->write( $domain , $filename , $content );echo($stor->read( $domain , $filename));//擷取檔案的內容echo ‘<br>‘;echo($stor->getUrl($domain,$filename));//擷取檔案的絕對位址?>

運行可以得到以下的結果(http://lazydemo.sinaapp.com/storage/know_storage.php)

hello lazyhttp://lazydemo-lazy.stor.sinaapp.com/remote_file.txt

經過了上面的例子相信大部分人都應該弄清楚了storage是什麼了,那麼下面再給一個執行個體講解如果從form表單中直接上傳檔案寫到storage中。

index.html

<html><title>Sae Storage demo</title><div><form id="pic_upload" action="recieve.php" method="POST" enctype="multipart/form-data" target="_self" name="pic_upload" action-type="form" node-type="form"><input type="file" name="file" class="file"  id="imgfile" value="demo" /><input type="submit" value="Upload" class="write_weibo" name="submit" /></form></div>

recieve.php

<?php$stor = new SaeStorage();$domain = ‘lazy‘;//我剛建立的domain的名稱$url = NULL;if($_FILES["file"]["tmp_name"] != NULL){$fileDataName = $_FILES["file"]["name"];//添加圖片上傳到STORAGE$dumpdata = file_get_contents($_FILES["file"]["tmp_name"]);$dowLoadUrl = $stor->write($domain,$fileDataName,$dumpdata);//用write就行了$url = $stor->getUrl($domain,$fileDataName);//如果上傳圖片的處理地址echo "上傳的檔案:";echo($url);}?>

實驗地址:http://lazydemo.sinaapp.com/storage/

關於用wrapper(saestor://)操作storage

sae提供了wrapper來操作storage,這樣方便file_get_contents等函數的使用,進一步降低了學習成本,下面我就用一個簡單的例子講敘如何使用saestor://來操作storage。見下面的執行個體:

<?phpfile_put_contents(‘saestor://lazy/testwrapper.txt‘,‘hello wrapper‘);//寫一個hello wrapper到testwrapper.txt檔案中$content = file_get_contents(‘saestor://lazy/testwrapper.txt‘);var_dump($content);?>

訪問:http://lazydemo.sinaapp.com/storage/storage_wrapper.php可以看到得到的結果是:

string(13) "hello wrapper"
本次教程所有源碼打包:

http://lazydemo.sinaapp.com/storage/storage.zip

Sina App Engine(SAE)入門教程(7)- Storage使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.