【问题标题】:Unable to Upload Images to GCS from Xcode无法从 Xcode 将图像上传到 GCS
【发布时间】:2014-07-01 11:43:59
【问题描述】:

我正在使用

- (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以上,我知道一些愚蠢的事情是一个问题,帮我找出来。谢谢:)

【问题讨论】:

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


    【解决方案1】:

    您确定这些文件没有上传,或者它们可能没有预期的权限,因此不可读? (不知bucket权限会不会有东西 与阅读部分有关)。 您是否在 App Engine 应用程序日志中看到与上传请求相关的任何内容? ACL_PUBLIC_READ 的值是“public-read”吗? 仅供参考,在您的情况下,如果所有数据都可用,最好这样写: gcsService.createOrReplace(文件名,选项,);

    【讨论】:

    • 是的,我检查了所有 GCS 权限等...我能够从浏览器点击上传图像。但是,应用程序之间通过 Web 服务进行通信是一个问题。多部分,图像上传只是问题。 :(
    猜你喜欢
    • 2021-12-09
    • 2017-07-29
    • 2014-01-12
    • 1970-01-01
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多