简体   繁体   English

将 Summernote 文件存储到 AWS s3

[英]Store Summernote files into AWS s3

I am using summernote rich editor in my Laravel project and I want to store attachments like images and videos in AWS s3 instead of my storing them in my project.我在我的Laravel项目中使用 Summernote 富编辑器,我想将图像和视频等附件存储在 AWS s3 中,而不是将它们存储在我的项目中。

This is what I have but it's not working am getting CORS policy from console log这就是我所拥有的,但从console log中获取CORS policy时它不起作用

    <?php
    $bucket = 'bucketNameHere';
    // $folder = 'summer';
    
    // these can be found on your Account page, under Security Credentials > Access Keys
    $accessKeyId = env('AWS_ACCESS_KEY_ID');
    $secret = env('AWS_SECRET_ACCESS_KEY');
    
    $policy = base64_encode(
        json_encode([
            // ISO 8601 - date('c'); generates uncompatible date, so better do it manually
            'expiration' => date('Y-m-d\TH:i:s.000\Z', strtotime('+2 days')),
            'conditions' => [['bucket' => $bucket], ['acl' => 'public-read'], ['success_action_status' => '201'], ['starts-with', '$key', '/']],
        ]),
    );
    
    $signature = base64_encode(hash_hmac('sha1', $policy, $secret, true));
    
    ?>

And This is Javascript scripts这是 Javascript 脚本

  $(document).ready(function() {
            $('#description').summernote({
                height: 500,
                callbacks: {
                    onImageUpload: function(files) {
                        for (let i = 0; i < files.length; i++) {
                            sendFile(files[i]);
                        }
                    }
                }
            });

            function sendFile(file, editor, welEditable) {
                formData = new FormData();
                formData.append('key', '/' + file.name);
                formData.append('AWSAccessKeyId', '<?php echo $accessKeyId; ?>');
                formData.append('acl', 'public-read');
                formData.append('policy', '<?php echo $policy; ?>');
                formData.append('signature', '<?php echo $signature; ?>');
                formData.append('success_action_status', '201');
                formData.append('file', file);

                $.ajax({
                    data: formData,
                    dataType: 'xml',
                    type: "POST",
                    cache: false,
                    contentType: false,
                    processData: false,
                    url: "https://<?php echo $bucket; ?>.s3.amazonaws.com/",
                    success: function(data) {
                        // getting the url of the file from amazon and insert it into the editor
                        var url = $(data).find('Location').text();
                        console.log(url);
                        editor.insertImage(welEditable, url);
                    }
                });
            }
        });

How can I achieve this?我怎样才能做到这一点?

I was able to store summernote files in aws s3 using the code snippet below in my Laravel Controller :我能够在我的Laravel Controller中使用下面的代码片段将summernote文件存储在aws s3中:

    $summernote = new Blog();

    $description = $request->description;

    $dom = new \DomDocument('1.0', 'UTF-8');
    libxml_use_internal_errors(true);

    $dom->loadHtml($description, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);    

    $images = $dom->getElementsByTagName('img');

    foreach($images as $k => $img){

        $data = $img->getAttribute('src');

        list($type, $data) = explode(';', $data);

        list($type, $data) = explode(',', $data);

        $data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $data));

        $filePath=  time().$k.'.png';

        Storage::disk('s3')->put($filePath, $data);
        
        $url = 'your-bucket/'.$filePath;

        $img->removeAttribute('src');

        $img->setAttribute('src',  $url);

     }

    $description = $dom->saveHTML();

    $summernote->description = $description;
    $summernote->save();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM