Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将base64编码的图像上载到S3 bucket php codeigniter_Php_Codeigniter - Fatal编程技术网

如何将base64编码的图像上载到S3 bucket php codeigniter

如何将base64编码的图像上载到S3 bucket php codeigniter,php,codeigniter,Php,Codeigniter,如何将base64映像上载到s3存储桶。我正在使用CodeIgniter 3,图像将通过Rest API发布到服务器上此代码对我有效。请检查它 This code works for me.Please check it <?php $image = $this->generateImage($_POST['foto']); public function generateImage($img) { $folderPath = "uploads/"; $imag

如何将base64映像上载到s3存储桶。我正在使用CodeIgniter 3,图像将通过Rest API发布到服务器上

此代码对我有效。请检查它
This code works for me.Please check it
<?php

$image = $this->generateImage($_POST['foto']);

public function generateImage($img)
{

    $folderPath = "uploads/";
    $image_parts = explode(";base64,", $img);
    $image_type_aux = explode("uploads/", $image_parts[0]);
    $image_base64 = base64_decode($image_parts[1]);
    $name = uniqid() . '.png';
    $file = $folderPath . $name;
    file_put_contents($file, $image_base64);
    $this->saveImageAmazomS3($name);

}

function saveImageAmazomS3($image)

    $filePath = base_url()."uploads/".$image;

    require 'vendor/autoload.php';

    $bucketName = 'YOUR_BUCKET_NAME';
    $filePath = './YOUR_FILE_NAME.png';
    $keyName = basename($filePath);
    $IAM_KEY = 'YOUR_SECRET_ACCESS_KEY';
    $IAM_SECRET = 'YOUR_SECRET_ACCESS_CODE';
    use Aws\S3\S3Client;
    use Aws\S3\Exception\S3Exception;
    // Set Amazon S3 Credentials
    $s3 = S3Client::factory(
        array(
            'credentials' => array(
                'key' => $IAM_KEY,
                'secret' => $IAM_SECRET
            ),
            'version' => 'latest',
            'region'  => 'us-east-2'
        )
    );

    try {
        if (!file_exists('/tmp/tmpfile')) {
            mkdir('/tmp/tmpfile');
        }

        // Create temp file
        $tempFilePath = '/tmp/tmpfile/' . basename($filePath);
        $tempFile = fopen($tempFilePath, "w") or die("Error: Unable to open file.");
        $fileContents = file_get_contents($filePath);
        $tempFile = file_put_contents($tempFilePath, $fileContents);


        // Put on S3
        $s3->putObject(
            array(
                'Bucket'=>$bucketName,
                'Key' =>  $keyName,
                'SourceFile' => $tempFilePath,
                'StorageClass' => 'REDUCED_REDUNDANCY'
            )
        );
    } catch (S3Exception $e) {
        echo $e->getMessage();
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}

最好先用谷歌搜索一下,或者添加一些你在这里尝试过的代码。。尝试了这个,图像上传到我的服务器上,但我需要直接存储在s3上//此链接有助于在AmazonS3中存储图像