我正在尝试通过发布请求将文件上传到amazon s3。麻烦的是,我不知道如何将请求格式化为多部分表单。
这就是我现在所拥有的:
content_type = "image/JPEG"
key = 'uploads/filename.jpg'
acl = "public-read"
bucket = None
params_raw = create_upload_data(content_type,key,acl,bucket)
params = { 'policy': params_raw['policy'],'acl':acl,'signature':params_raw['signature'],'key':params_raw['key'],'Content-Type':params_raw['Content-Type'],'AWSAccessKeyId':params_raw['AWSAccessKeyId'],'success_action_status':params_raw['success_action_status'],'binary': binary_data}
r = requests.post(params_raw['form_action'],data=params)
我认为我收到了错误的回复,因为它不是一个多部分表单,但这里是响应文本的样子:
{"request": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>InvalidArgument</Code><Message>Conflicting query string parameters: acl, policy</Message><ArgumentName>ResourceType</ArgumentName><ArgumentValue>acl</ArgumentValue><RequestId>D558E016151E448F</RequestId><HostId>WT5aT0OOqJx3ziPgYFzjuTHJSERaCcuJG+y/acs6+l/mWVwO0MiH3lhWyBWIdhKr9BnhdIpkarw=</HostId></Error>"}
如何使用文件构建请求...它是基础64中的.jpg?
答案 0 :(得分:0)
更改内容类型
就足够了package com.example.akibmahmud.namota;
import android.app.AlertDialog;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements MediaPlayer.OnCompletionListener {
EditText et1,et2;
private Button bt;
ScrollView sv;
TextView tv;
private MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1= (EditText) findViewById(R.id.firsteditText);
et2= (EditText) findViewById(R.id.secondeditText);
bt= (Button) findViewById(R.id.button);
sv= (ScrollView) findViewById(R.id.scrollView);
tv= (TextView) findViewById(R.id.textView);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String et11=et1.getText().toString();
String et22=et2.getText().toString();
if (!et11.equals("") && !et22.equals("")){
int input1=Integer.parseInt(et11);
int input2=Integer.parseInt(et22);
int i,k,r;
for (i = input1; i <=input1 ; i++) {
for (k =input2; k <= 50; k++) {
tv.setText(+i+"*"+k+"="+i*k+"/n");
}
}
}else{
play();
}
}
});
setup();
}
private void play(){
mp.start();
bt.setEnabled(false);
}
private void loadClip(){
try {
mp=MediaPlayer.create(this,R.raw.ok);
mp.setOnCompletionListener(this);
}catch (Throwable t){
goBlooey(t);
}
}
private void setup(){
loadClip();
bt.setEnabled(true);
}
private void goBlooey(Throwable t){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder
.setTitle("Exception!")
.setMessage(t.toString())
.setPositiveButton("OK",null)
.show();
}
@Override
public void onCompletion(MediaPlayer mp) {
setup();
}
}
答案 1 :(得分:0)
在这方面有一点点痛苦,但终于明白了。最后非常简单!
url = "https://yourbucket.s3.amazonaws.com"
#complete_path is the local server path to the file
files = {'file':open(complete_path,'rb')}
r = requests.post(url, data=params, files=files)