繁体   English   中英

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

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

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

帮助或批评(iOS,PHP等)将非常感激!!!

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

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

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