简体   繁体   English

使用PHP从Google Cloud Storage上传和检索图像

[英]Upload and Retrieve Image from Google Cloud Storage in PHP

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. 我正在尝试从iPhone应用程序中获取上传的图像,然后使用PHP将其存储到我的Google Cloud Storage存储桶(gs:// app / users / profile_pics /)中。 I was able to do this with the local file system but it isn't working with Google Cloud Storage. 我可以使用本地文件系统执行此操作,但是它不适用于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');
...

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. 我知道其余的php都可以正常工作,因为我将配置文件保存到了Google Cloud SQL数据库中。

An help or critique (iOS, PHP, etc.) would be very appreciative!!! 帮助或批评(iOS,PHP等)将非常感激!!!

The cloud storage is not on the same machine as the appengine. 云存储与appengine不在同一台计算机上。 You'll never be able to use the local filesystem on appengine. 您将永远无法在appengine上使用本地文件系统。

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. 我看不到php示例,但是您可以引用JSON API与存储进行交互。 You should be able to use Buzz to do a simple upload 您应该可以使用Buzz进行简单的上传

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

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

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