Last active
August 29, 2015 14:01
-
-
Save yoshimax/7d180a46f8b3ab60925d to your computer and use it in GitHub Desktop.
Papercilp Setup Memo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1.AWS S3 Make Bucket | |
2.AWS S3 Setup User | |
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Action": "s3:*", | |
"Resource": [ | |
"arn:aws:s3:::NAME”, | |
"arn:aws:s3:::NAME/*” | |
] | |
}, | |
{ | |
"Effect": "Allow", | |
"Action": "s3:ListAllMyBuckets", | |
"Resource": "arn:aws:s3:::*" | |
} | |
] | |
} | |
3. Modify Bucket Permission | |
Authenticated Users | |
4. AWS | |
$ sudo yum install ImageMagick | |
$ sudo yum install ImageMagick-devel | |
5. Ruby | |
Gemfile | |
gem 'paperclip' | |
gem 'aws-sdk' | |
$bundle install --path=vendor/bundle --binstubs=vendor/bin | |
5. Rails Table | |
$ bundle exec rails generate migration user_avater | |
add_attachment :users, :avatar | |
$ bundle exec rake db:migrate | |
6. Rails User.rb | |
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png", :path => "/:class/:attachment/:id_partition/:style/avatar.:extension" | |
validates_attachment :avatar, | |
:content_type => { :content_type => ["image/jpeg", "image/gif", "image/png"] } | |
$ bundle exec rails generate migration user_avater | |
bundle exec rake db:migrate | |
7. Rails Controller | |
def upload_image_avatar | |
if current_user.update(upload_image) | |
render :json=> {'message' => '', | |
'avatar_m' => current_user.avatar.url(:medium), | |
'avatar_s' => current_user.avatar.url(:thumb)}, :status=>201 | |
return | |
else | |
render :json=> {'message' => current_user.errors.full_messages}, :status=>422 | |
return | |
end | |
end | |
8. Rails routes | |
config/routes.rb | |
post 'users/upload_image_avatar' | |
9. Paperclip | |
config/initializers/paperclip.rb | |
Paperclip::Attachment.default_options[:storage] = :s3 | |
Paperclip::Attachment.default_options[:s3_protocol] = 'http' | |
Paperclip::Attachment.default_options[:s3_credentials] = { :bucket => ‘NAME’, | |
:access_key_id => ‘KEY’, | |
:secret_access_key => ‘KEY’ } | |
Paperclip::Attachment.default_options[:url] = ':s3_domain_url' | |
Paperclip::Attachment.default_options[:s3_host_name] = 's3-ap-southeast-1.amazonaws.com' | |
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename' | |
B. iOS | |
- (void)uploadImage:(UIImage *)image { | |
[SVProgressHUD show]; | |
NSString *URLString = [NSString stringWithFormat:@"%@%@", [ApiUtil host], @"api/v1/users/upload_image_avatar"]; | |
NSDictionary *parameters = @{@"client_id":[ApiUtil client_id], @"client_secret":[ApiUtil client_secret]}; | |
NSString *imageName = @"tmp.jpg"; | |
NSData *imageData = UIImageJPEGRepresentation(image, 0.8); | |
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { | |
[formData appendPartWithFileData:imageData name:@"user[avatar]" fileName:imageName mimeType:@"image/jpeg"]; | |
}]; | |
[request setValue:[NSString stringWithFormat:@"Bearer %@", [[ApiUtil sharedInstance] token]] forHTTPHeaderField:@"Authorization"]; | |
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request]; | |
op.responseSerializer = [AFJSONResponseSerializer serializer]; | |
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { | |
[SVProgressHUD dismiss]; | |
[ApiUtil sharedInstance].avatar_m = (responseObject[@"avatar_m"] == [NSNull null]) ? @"" : responseObject[@"avatar_m"]; | |
[ApiUtil sharedInstance].avatar_s = (responseObject[@"avatar_s"] == [NSNull null]) ? @"" : responseObject[@"avatar_s"]; | |
if ( [ApiUtil sharedInstance].avatar_s ) { | |
[self.profileImage setImageWithURL:[NSURL URLWithString:[ApiUtil sharedInstance].avatar_s] placeholderImage:nil]; | |
} | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
[SVProgressHUD dismiss]; | |
if (operation.response.statusCode == 401) { | |
[self errorAuthMessage:@"お手数ですが時間を置いて再度実行してください"]; | |
}else{ | |
[self errorAuthMessage:@"お手数ですが時間を置いて再度実行してください"]; | |
} | |
}]; | |
[[NSOperationQueue mainQueue] addOperation:op]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment