使用PHP和Ajax将文件上传到Google Cloud Storage Bucket [英] Upload a file to Google Cloud Storage Bucket with PHP and Ajax

查看:107
本文介绍了使用PHP和Ajax将文件上传到Google Cloud Storage Bucket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个PHP项目中,我需要在其中将文件(由HTML表单提供)上传到Google Cloud Storage Bucket.表单请求将作为ajax请求接收.我的文件上传成功,但是在存储桶中,它显示了一个临时文件,但是我已经上传了一个图像文件.

I'm working on a PHP project in which I need to upload a file (provided by an HTML form) to the Google Cloud Storage Bucket. The form request will be received as an ajax request. My file is uploading successfully but inside the bucket, it shows a temp file but I have uploaded an image file.

这是我尝试过的:

PHP文件:

if(isset($_FILES['file'])){
    $errors= array();
    // Authentication with Google Cloud Platform
    $client = new StorageClient(['keyFilePath' => 'api-project-374381085870-eaf930d9ffd7.json']);
    $bucket = $client->bucket('storage_client');

    // Upload a file to the bucket.
    $bucket->upload(
        fopen($_FILES['file']['name'], 'r')
    );
}

Ajax代码:

    $(document).on('submit', '#fileForm', function (e) {
    e.preventDefault();
    var data = new FormData($('#fileForm').get(0));
    $.ajax({
        type: 'POST',
        url: 'gcp_storage.php',
        data: data,
        processData: false,
        contentType: false,
        success: function ($data) {
            console.log('Succcess');
            $('#success').attr('hidden', false);
        }
    });
});

HTML代码:

<form id="fileForm" action="" method="post" enctype="multipart/form-data">
    <div class="form-row">
        <br/>
        <div class="form-group">
            <label for="textFile">Text File: </label>
            <br/>
            <input type="file" name="file" id="textFile">
        </div>
    </div>
    <br>
    <button type="submit" class="btn btn-lg btn-primary"> Upload </button>
</form>

以下是图像文件在存储桶中的外观:

Here's how an image file looks inside bucket:

更新:

现在,我已更改:

:

// Upload a file to the bucket.
    $bucket->upload(
        fopen($_FILES['file']['name'], 'r')
    );

收件人:

// Upload a file to the bucket.
$bucket->upload(
    fopen($_FILES['file']['tmp_name'], 'r')
);

文件上传成功,但文件扩展名未在存储桶内显示.因此,在上传到Google云存储分区之前,有什么方法可以重命名文件吗?

It's uploading the files successfully but the file extension doesn't display inside the bucket. So, is there a way I can rename the file before uploading to google cloud storage bucket?

请帮帮我!

提前谢谢!

推荐答案

添加带有名称的$ options参数可以解决该问题.

add $options params with name solve the problem.

$bucket->upload(
    fopen($_FILES['file']['tmp_name'], 'r'),
    ['name' => 'some-name.jpg']
);    

更多信息可在源代码中找到:

More info can be found in the source code: https://github.com/GoogleCloudPlatform/google-cloud-php/blob/master/Storage/src/Bucket.php

更多信息:

$ bucket->上传的结果是 StorageObject . 您可以通过以下方式获取签名的URL:

the result of $bucket->upload is an StorageObject. You can get the signed URL through:

$obj = $bucket->upload(...);
$url = $obj->signedUrl(new Timestamp(new DateTime('tomorrow'))); 

更多信息可以在 https://github中找到.com/GoogleCloudPlatform/google-cloud-php/tree/master/Storage/src .

已签名URL的信息: https://cloud.google. com/storage/docs/access-control/signed-urls

information of signed URLS: https://cloud.google.com/storage/docs/access-control/signed-urls

这篇关于使用PHP和Ajax将文件上传到Google Cloud Storage Bucket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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