1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

S3 にファイルアップロード

Posted at

いろいろ定義:

bootstrap.php
define('AWS_ACCESS_KEY', 'YOUR AWS ACCESS KEY');
define('AWS_ACCESS_SECRET', 'YOUR AWS ACCESS SECRET');
define('BUCKET_NAME', 'YOUR BUCKET NAME');

S3 にアップロードするクラス:

Upload.php
private static function uploadToS3($filePath, $mimeType)
{
	$s3 = Services_Amazon_S3::getAccount(AWS_ACCESS_KEY, AWS_ACCESS_SECRET);
	$bucket = $s3->getBucket(BUCKET_NAME);
	
	$s3FilePath = date('Y/md/His') . basename($filePath);
	$object = $bucket->getObject($s3FilePath);
	$object->contentType = $mimeType;
	$object->acl = Services_Amazon_S3_AccessControlList::ACL_PUBLIC_READ;
	$object->data = file_get_contents($filePath);
	$object->save();
	
	$url = $object->getUrl();
	//$url = str_replace('%2F', '/', $url);
	return $url;
}

上のクラスを使うコントローラクラス:

ExampleController.php
	Upload::uploadToS3('example.jpg', 'image/jpeg');
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?