/** * Wrapper for best practices in putting an object. * @param array $params: Bucket, Prefix, SourceFile, Key (filename) * @return string URL of the uploaded object in s3 */ public function saveObjectInBucket($params = array()) { $error = null; // Create bucket try { $this->createBucket(array('Bucket' => $params['Bucket'])); } catch (Exception $e) { throw new Exception("Something went wrong creating bucket for your file.\n" . $e); } // Poll the bucket until it is accessible try { $this->waitUntil('BucketExists', array('Bucket' => $params['Bucket'])); } catch (Exception $e) { throw new Exception("Something went wrong waiting for the bucket for your file.\n" . $e); } // Upload an object $file_key = $params['Prefix'] . '/' . $params['Key']; $path = pathinfo($file_key); $extension = $path['extension']; $mimes = new Guzzle\Http\Mimetypes(); $mimetype = $mimes->fromExtension($extension); try { $aws_object = $this->saveObject(array('Bucket' => $params['Bucket'], 'Key' => $file_key, 'ACL' => 'public-read', 'SourceFile' => $params['SourceFile'], 'ContentType' => $mimetype))->toArray(); } catch (Exception $e) { throw new Exception("Something went wrong saving your file.\n" . $e); } // We can poll the object until it is accessible try { $this->waitUntil('ObjectExists', array('Bucket' => $params['Bucket'], 'Key' => $file_key)); } catch (Exception $e) { throw new Exception("Something went wrong polling your file.\n" . $e); } // Return result return $aws_object['ObjectURL']; }
function mimeTypeFromString($extension) { $mimetypes = new \Guzzle\Http\Mimetypes(); $mime = $mimetypes->fromExtension($extension); return $mime; }