<?php include "SCS.php"; include "config.php"; if (count($argv) <= 1) { echo "ERROR\n"; exit; } $uploadFile = $argv[1]; $uploadFile = realpath($uploadFile); echo "Upload " . $uploadFile . "\n"; if (!file_exists($uploadFile)) { echo "FILE NOT EXIST\n"; exit; } $scs = new SCS(AccessKey, SecretKey); $bucketName = BUCKETNAME; $m = $scs->putObjectFile($uploadFile, $bucketName, PREFIX . baseName($uploadFile), SCS::ACL_PUBLIC_READ); if ($m == true) { echo "UPLOAD OK ! \n"; } else { echo "UPLOAD FAILED ! \n"; }
exit("\nERROR: CURL extension not loaded\n\n"); } // Pointless without your keys! if (AccessKey == 'change-this' || SecretKey == 'change-this') { exit("\nERROR: access information required\n\nPlease edit the following lines in this file:\n\n" . "define('AccessKey', 'change-me');\ndefine('SecretKey', 'change-me');\n\n"); } // Instantiate the class $scs = new SCS(AccessKey, SecretKey); echo "SCS::getAuthenticatedURL(): " . SCS::getAuthenticatedURL('sdk', 'snapshot/snapshot.png', 86400000) . "\n"; // List your buckets: echo "SCS::listBuckets(): " . print_r($scs->listBuckets(), 1) . "\n"; // Create a bucket with public read access if ($scs->putBucket($bucketName, SCS::ACL_PUBLIC_READ)) { echo "Created bucket {$bucketName}" . PHP_EOL; // Put our file (also with public read access) if ($scs->putObjectFile($uploadFile, $bucketName, baseName($uploadFile), SCS::ACL_PUBLIC_READ)) { echo "SCS::putObjectFile(): File copied to {$bucketName}/" . baseName($uploadFile) . PHP_EOL; // Get the contents of our bucket $contents = $scs->getBucket($bucketName); echo "SCS::getBucket(): Files in bucket {$bucketName}: " . print_r($contents, 1); // Get object info $info = $scs->getObjectInfo($bucketName, baseName($uploadFile)); echo "SCS::getObjectInfo(): Info for {$bucketName}/" . baseName($uploadFile) . ': ' . print_r($info, 1); // You can also fetch the object into memory // var_dump("SCS::getObject() to memory", $scs->getObject($bucketName, baseName($uploadFile))); // Or save it into a file (write stream) // var_dump("SCS::getObject() to savefile.txt", $scs->getObject($bucketName, baseName($uploadFile), 'savefile.txt')); // Or write it to a resource (write stream) // var_dump("SCS::getObject() to resource", $scs->getObject($bucketName, baseName($uploadFile), fopen('savefile.txt', 'wb'))); // Get the access control policy for a bucket: // $acp = $scs->getAccessControlPolicy($bucketName);
/** * 上传附件 * * @static * @access public * @return bool * @throws Typecho_Plugin_Exception */ public static function ScsUpload($file, $content = null) { if (empty($file['name'])) { return false; } //获取安全的文件名后缀 $ext = self::getSafeName($file['name']); if (!Widget_Upload::checkFileType($ext)) { return false; } $option = self::getSCSconfig(); $date = new Typecho_Date(Typecho_Widget::widget('Widget_Options')->gmtTime); $path = $option->format == null ? $date->year . '/' . $date->month . '/' : preg_replace(array('/\\{year\\}/', '/\\{month\\}/', '/\\{day\\}/'), array($date->year, $date->month, $date->day), self::getSCSFilepath($option->format)); /*非必须(在本地附件目录/usr/uploads/下创建新目录) if (!is_dir($path)) { if (!self::makeUploadDir($path)) { return false; } } */ //以Unix time stamp形式储存:$path .= sprintf('%u', crc32(uniqid())) . '.' . $ext; $path .= $file['name']; if (isset($content)) { $path = $content['attachment']->path; self::ScsDelete($path); } $clienttmp = $file['tmp_name']; if (!isset($clienttmp)) { return false; } self::getSCSsdk(); $scs = new SCS($option->accesskey, $option->secretkey); if ($scs->putObjectFile($clienttmp, $option->bucket, $path, SCS::ACL_PUBLIC_READ)) { return array('name' => $file['name'], 'path' => $path, 'size' => $file['size'], 'type' => $ext, 'mime' => Typecho_Common::mimeContentType($path)); } else { return false; } }