Created
December 18, 2013 07:29
-
-
Save ericsk/8018606 to your computer and use it in GitHub Desktop.
使用 Windows Azure SDK for PHP 將上傳的檔案上傳至 Windows Azure Blob
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require_once 'vendor/autoload.php'; | |
| use WindowsAzure\Common\ServicesBuilder; | |
| use WindowsAzure\Blob\Models\CreateContainerOptions; | |
| use WindowsAzure\Blob\Models\CreateBlobOptions; | |
| use WindowsAzure\Blob\Models\PublicAccessType; | |
| use WindowsAzure\Common\ServiceException; | |
| $storageAccount = "填入你的 Windows Azure 儲存體名稱"; | |
| $storagePrimaryKey = "填入對應的管理金鑰"; | |
| $connectionString = "DefaultEndpointsProtocol=https;AccountName=$storageAccount;AccountKey=$storagePrimaryKey"; | |
| # 建立 Blob 服務的用戶端 | |
| $blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString); | |
| # 檔案名稱 | |
| $path = md5(strval(time())).".jpg"; | |
| $file = fopen($_FILES['file']['tmp_name'], 'r'); | |
| # 設定上傳後的 content type | |
| $createBlobOptions = new CreateBlobOptions(); | |
| $createBlobOptions->setBlobContentType("image/jpeg"); | |
| # 容器名稱 | |
| $containerName = "assets"; | |
| try { | |
| # 上傳檔案內容 | |
| $blobRestProxy->createBlockBlob($containerName, $path, $file, $createBlobOptions); | |
| # 上傳完成,產生可存取的 URL | |
| $url = "https://$storageAccount.blob.core.windows.net/$containerName/$path"; | |
| } catch (ServiceException $e) { | |
| # 處理錯誤 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment