无法从Xcode将图像上传到GCS

时间:2014-07-01 11:43:59

标签: objective-c google-app-engine ios7 uiimagepickercontroller multipartform-data

我正在使用

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

选择" UIImagePickerControllerOriginalImage" ,然后我只是压缩并上传到GCS。

-(void) saveImage: (NSData *)imageData forImageName: (NSString *) imageName 
{

    id customerObj=[[[SSAppCache cache]getObject:@"customerKey"].value JSONValue];

    NSString *requestURL = [[NSString alloc] initWithFormat:@"%@",dbURL];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.requestSerializer = [AFHTTPRequestSerializer serializer];

    NSString *imagePostUrl = [NSString stringWithFormat:@"%@/CustomerProfileService/uploadProfileImageAndUpdate", requestURL];
    NSDictionary *parameters = @{@"email": [customerObj valueForKeyPath:@"emailId"] , @"custId":[customerObj valueForKeyPath:@"customerId"]};

    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:imagePostUrl parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

    double compressionRatio = 1.0;
    NSData *imgData=UIImageJPEGRepresentation(defaultImage,compressionRatio);
    while ([imgData length]>50000) {
        compressionRatio=compressionRatio*0.5;
        imgData=UIImageJPEGRepresentation(defaultImage,compressionRatio);
    }
    [formData appendPartWithFileData:imgData name:@"fileBytes" fileName:imageName mimeType:@"image/jpeg"];

}];
[request addValue:@"multipart/form-data" forHTTPHeaderField: @"Content-Type"];
DLog(@"Request URL is:%@",request);
AFHTTPRequestOperation *op = [manager HTTPRequestOperationWithRequest:request success: ^(AFHTTPRequestOperation *operation, id responseObject) {
    DLog(@"response: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    DLog(@"Error: %@ operation Response code:%ld" , error,(long)[operation.response statusCode]);
}];
op.responseSerializer = [AFHTTPResponseSerializer serializer];
[[NSOperationQueue mainQueue] addOperation:op];
}

我的java服务方法如下,通过

      public String uploadProfileImageAndUpdate(String email,String custId ,byte[] fileBytes) throws Exception
    {
      ...
     String filepath = GoogleStorageUtil.uploadFile(fileBytes, "<BUCKET DETAILS>", "<FOLDER>"+fileName, "image/jpeg");

    }

但是,它甚至没有被击中..... :(

  public static String uploadUsingGcs(byte[] fileBytes,String bucketName,String      objectName,String mimeType)
   {
    String filePath = null;
    try
    {
        mLogger.info("into the method to upload file "+objectName+" to "+bucketName);
        filePath=GOOGLE_CLOUD_STORAGE_URL+"/"+bucketName+"/"+objectName;
        GcsService gcsService = GcsServiceFactory.createGcsService(new RetryParams.Builder().initialRetryDelayMillis(10).retryMaxAttempts(10).totalRetryPeriodMillis(15000).build());
        GcsFilename filename = new GcsFilename(bucketName, objectName);
        GcsFileOptions options = new GcsFileOptions.Builder().cacheControl("max-age=31557600").mimeType(mimeType).acl(ACL_PUBLIC_READ).build();
        GcsOutputChannel writeChannel = gcsService.createOrReplace(filename,options);
        writeChannel.write(ByteBuffer.wrap(fileBytes));
        writeChannel.close();
        mLogger.info("File successfully uploaded");
    }
    catch(Exception e){
        mLogger.warning("The error came becoz of "+e.getMessage());
        for(StackTraceElement s:e.getStackTrace()){
            mLogger.warning(s.toString());
        }
    }
    return filePath;
}

我无法上传图片,我尝试了所有类型,如Multipart,Json,String等,没有任何工作。没有错误,警告等...它的冷静:(我的目标ios版本7.0以上,我知道一些愚蠢的事情是一个问题,帮我找出答案。谢谢:)。

1 个答案:

答案 0 :(得分:0)

您确定文件没有上传,或者他们没有获得预期的权限,因此无法读取? (我想知道桶的许可是否有所帮助 与阅读部分有关)。 您是否在App Engine应用程序日志中看到与上传请求相关联的内容? ACL_PUBLIC_READ的值是&#34; public-read&#34;? 仅供参考,在您的情况下,所有数据都可用,最好以这种方式编写: gcsService.createOrReplace(filename,options,);