使用PHP从Google Cloud Storage上传和检索图像 [英] Upload and Retrieve Image from Google Cloud Storage in PHP

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

问题描述

我对这一切都很陌生.我正在尝试从iPhone应用程序中获取上传的图像,然后使用PHP将其存储到我的Google Cloud Storage存储桶(gs://app/users/profile_pics/)中.我可以使用本地文件系统执行此操作,但是它不适用于Google Cloud Storage.

I am very new to all of this. I am trying to take an uploaded image from my iPhone app and store it into my Google Cloud Storage bucket (gs://app/users/profile_pics/) using PHP. I was able to do this with the local file system but it isn't working with Google Cloud Storage.

**iOS Code (this works)**
...
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:profile_pic_data]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
...

**PHP Code to local file system (works using local file system)**
if($_POST) {
...
    //Make user directory to house files
    $upload_dir = 'user_data/' . $user_id . '/'; 
    $path = mkdir("$upload_dir", 0777);

    //Save file to newly created folder
    $file = 'profile_pic.jpg';
    $profile_pic_path = $upload_dir . $file;
    if (move_uploaded_file($_FILES['file']['tmp_name'], $profile_pic_path)) {
        //Profile pic uploaded, Update profile pic field in database
        $query = "UPDATE users SET profile_image_path='$upload_path' WHERE email='$user_email'";
        if(mysql_query($query)){
            echo json_encode(array('success' => 1,'info_message' => "")); // Account fully created!
        }
...

**PHP using Google Cloud Storage (not working, trying the most simple version first)**
...
if($_POST) {
...
    $gs_name = $_FILES['file']['tmp_name'];
    move_uploaded_file($gs_name, 'gs://socalityapp/users/profile_pics/profile_pic.jpg');
...

我似乎无法将其存储.我知道其余的php都可以正常工作,因为我将配置文件保存到了Google Cloud SQL数据库中.

I can't seem to get it to store. I know the rest of my php is working because I am saving the profiles into my Google Cloud SQL database.

非常有帮助或批评(iOS,PHP等)!

An help or critique (iOS, PHP, etc.) would be very appreciative!!!

推荐答案

云存储与appengine不在同一台计算机上.您将永远无法在Appengine上使用本地文件系统.

The cloud storage is not on the same machine as the appengine. You'll never be able to use the local filesystem on appengine.

当您收到带有表单提交的帖子时,需要将图像的数据向前传递到存储中.我看不到php示例,但是您可以引用JSON API与存储进行交互.您应该可以使用 Buzz 进行

When you receive the post with the form submission, you need to pass that data of the image onwards to the storage. I could not see a php example, but you can reference the JSON API to interact with the storage. You should be able to use Buzz to do a simple upload

POST https://www .googleapis.com/upload/storage/v1/b/myBucket/o?uploadType = media& name = myObject

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

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