Laravel S3图片上传会自动创建一个带有文件名的文件夹 [英] Laravel S3 image upload creates a folder with the filename automatically

查看:155
本文介绍了Laravel S3图片上传会自动创建一个带有文件名的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel 5.4.*.我在帮助程序文件中有此简单代码,可以在名为"instant_gifs/"的文件夹下的S3存储桶中上传图像/gif.代码如下:

I'm using Laravel 5.4.*. I've this simple code in a helper file to upload images/gif in S3 bucket under a folder named say "instant_gifs/". The code is below:

if ( !function_exists('uploadFile') ) {

function uploadFile($fileContent, $fileName, $size='full', $disk='s3')
{
    $rv = '';
    if( empty($fileContent) ) {
        return $rv;
    }

    if($size == 'full') {
        dump($fileName);

        $path = Storage::disk($disk)->put(
                    $fileName,
                    $fileContent,
                    'public'
                );
    }

    if ( $path ) {
        $rv = $fileName;
    }

    return $rv;
}

}

从控制器中,我正在调用如下的helper方法:

From the controller, I'm calling the helper method as below:

$file = $request->gif;
$file_name = 'instant_gifs/' . $user_id . '_' . time() . '_' . $file->getClientOriginalName();
$result = uploadFile($file, $file_name);

在helper方法的$ fileName参数中,我以这种格式提供文件名,例如:

In the the $fileName parameter of the helper method, I'm providing the fileName as for example in this format:

"instant_gifs/83_1518596022_giphy.gif"

"instant_gifs/83_1518596022_giphy.gif"

但是在上传之后,我看到文件被存储在此文件夹下

but after the upload, I see that the file gets stored under this folder

"vvstorage/instant_gifs/83_1518596022_giphy.gif/CRm1o1YEcvX3fAulDeDfwT7DIMCxOKG8WFGcA3lB.gif"

"vvstorage/instant_gifs/83_1518596022_giphy.gif/CRm1o1YEcvX3fAulDeDfwT7DIMCxOKG8WFGcA3lB.gif"

具有随机文件名

CRm1o1YEcvX3fAulDeDfwT7DIMCxOKG8WFGcA3lB.gif

CRm1o1YEcvX3fAulDeDfwT7DIMCxOKG8WFGcA3lB.gif

根据代码,应该将其存储在以下路径中:

Whereas, according to the code, it should get stored in this path:

"vvstorage/instant_gifs/83_1518596022_giphy.gif"

"vvstorage/instant_gifs/83_1518596022_giphy.gif"

没有得到任何解释为什么会这样.任何线索将不胜感激.

Doesn't get any explanation why this is happening. Any clue will be appreciated.

BucketName = vvstorage
我要模仿的文件夹= Instant_gifs

BucketName = vvstorage
Folder I'm mimicking = instant_gifs

推荐答案

经过研究&测试,发现了问题. put()方法期望将第二个参数作为文件内容或流而不是文件对象.在我的代码中,我以$file = $request->gif;$file = $request->file('gif');的形式发送文件,希望Storage类将隐式获取文件内容.但是要获得预期的结果,我需要从控制器中调用如下的helper方法.请注意 file_get_contents()部分.

After some research & testing, found the issue. put() method expects the 2nd parameter as the file contents or stream not the file object. In my code, I was sending the file as $file = $request->gif; or $file = $request->file('gif'); hoping that Storage class will implicitly get the file contents. But to get the expected result, I needed to call the helper method from the controller as below. Notice the file_get_contents() part.

$file = $request->gif;
$file_name = 'instant_gifs/' . $user_id . '_' . time() . '_' . $file>getClientOriginalName();
$result = uploadFile( file_get_contents($file), $file_name );

现在,我已将图像正确存储在正确的路径下,例如在/instant_gifs/9_1518633281_IMG_7491.jpg中.

Now, I got the image correctly stored under the correct path for example in /instant_gifs/9_1518633281_IMG_7491.jpg.

现在,让我比较/总结实现相同结果的可用方法:

Now, let me compare/summarize the available methods for achieving the same result:

1) put():

1) put():

 $path = Storage::disk('s3')->put(
                    '/instant_gifs/9_1518633281_IMG_7491.jpg', #$path
                    file_get_contents($request->file('gif')), #$fileContent
                   'public' #$visibility

将其存储在/vvstorage/instant_gifs/9_1518633281_IMG_7491.jpg

2) putFileAs():要实现相同的目标putFileAs()的东西,我需要编写如下.第一个参数需要目录名称,我在通过文件名模仿s3中的目录名称时将其留空.

2) putFileAs(): To achieve the same thing withputFileAs(), I needed to write it as below. 1st parameter expects the directory name, I left it blank as I'm mimicking the directory name in s3 through the filename.

$path = Storage::disk('s3')->putFileAs(
                '', ## 1st parameter expects directory name, I left it blank as I'm mimicking the directory name through the filename
                '/instant_gifs/9_1518633281_IMG_7491.jpg',
                $request->file('gif'), ## 3rd parameter file resource
                ['visibility' => 'public'] #$options
            );

将其存储在/vvstorage/instant_gifs/9_1518633281_IMG_7491.jpg

3) storeAs():

$path = $request->file('gif')->storeAs(
             '', #$path
             '/instant_gifs/9_1518633281_IMG_7491.jpg', #$fileName
             ['disk'=>'s3', 'visibility'=>'public'] #$options
             );    

将其存储在/vvstorage/instant_gifs/9_1518633281_IMG_7491.jpg

其他:: 4)用于通过put()存储缩略图. stream()的示例...

Extras:: 4) For storing Thumbnails through put(). Example of stream() ...

$imgThumb = Image::make($request->file('image'))->resize(300, 300)->stream(); ##create thumbnail
        $path = Storage::disk('s3')->put(
                    'profilethumbs/' . $imgName,
                    $imgThumb->__toString(),
                    'public'
                );

希望它对某人有帮助.

Hope that it helps someone.

这篇关于Laravel S3图片上传会自动创建一个带有文件名的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆