【问题标题】:Upload a file to Google Cloud Storage Bucket with PHP and Ajax使用 PHP 和 Ajax 将文件上传到 Google Cloud Storage Bucket
【发布时间】:2023-03-14 05:09:01
【问题描述】:

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

这是我尝试过的:

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>

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

更新:

现在,我变了:

这个:

// 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')
);

文件上传成功,但文件扩展名不显示在存储桶内。那么,有没有办法在上传到谷歌云存储桶之前重命名文件?

请帮帮我!

提前致谢!

【问题讨论】:

    标签: javascript php ajax google-cloud-storage jquery-file-upload


    【解决方案1】:

    添加带有名称的 $options 参数解决问题。

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

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

    更多信息:

    $bucket->upload 的结果是一个StorageObject。 您可以通过以下方式获取签名 URL:

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

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

    签名网址信息:https://cloud.google.com/storage/docs/access-control/signed-urls

    【讨论】:

    • 嗨@Dat_Tran,我可以在上传到谷歌云存储桶后获取文件网址吗?
    • 我已经更新了答案,添加了有关 URL 的信息。如果您发现它解决了您的问题,请接受我的回答。
    【解决方案2】:

    您可以使用 pathinfo() 来获取文件名和扩展名。

    $file = pathinfo($_FILES['file']['name']);
    $prefix = uniqid('prefix_', true); //generate random string to avoid name conflicts
    $filename = $prefix . "." . $file['extension'];
    
    $bucket->upload(
      fopen($_FILES['file']['tmp_name'], 'r'),
      ['name' => $filename]
    );  
    

    这不仅将扩展名为 GCP 的文件保存到 GCP 存储桶,而且会为每个文件生成随机名称以避免名称冲突

    【讨论】:

      猜你喜欢
      • 2020-06-13
      • 1970-01-01
      • 2020-02-17
      • 2018-10-31
      • 1970-01-01
      • 2019-02-26
      • 2018-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多