###前提・実現したいこと
ESP8266でマイクで集音したデータをAWSで機械学習させる仕組みを構築しています。
録音したwavファイルをHTTPでS3にPOSTしようとしていますが、エラーになってしまいます。
POSTのテストツール(Advanced REST client)で同様のリクエストを投げてみたところ「412: Precondition Failed」が帰ってきました。
おそらく、http://docs.aws.amazon.com/fr_fr/AmazonS3/latest/API/sig-v4-authenticating-requests.html#auth-methods-intro
に記載のあるとおり、認証用の署名の添付が不足していたと考えています。
http://docs.aws.amazon.com/fr_fr/AmazonS3/latest/API/sigv4-auth-using-authorization-header.html
http://docs.aws.amazon.com/fr_fr/AmazonS3/latest/API/sig-v4-header-based-auth.html
に記載に沿ってAuthorizationヘッダをつけようと考えましたが、署名作成時の正規リクエスト作成時のクエリストリング、ヘッダなどのに入れるべき情報がわかりません。
プロトタイプ作成で特に必要以上のセキュリティは考えていないのですが最低限ファイル転送するためにS3にPOSTする際のHTTPヘッダに必要な情報、またその生成の仕方を教えていただきたいです。
###ソースコード
include <ESP8266WiFi.h>
include <FS.h>
include <ESP8266HTTPClient.h>
define BUFBIT 12
define BUFSIZE (1<<BUFBIT)
define BUFMASK (BUFSIZE-1)
define FREQUENCY 8000
define DURATION 16000
define HOSTNAME '<backetname>.s3-ap-northeast-1.amazonaws.com'
const char* ssid = "<ssid>";
const char* password = "<password>";
String payload = "";
int val = 0; // 読み取った値を格納する変数
int adc_bias;
int16_t buffer[16000];
int bitsize = 16;
File fd;
WiFiClient client;
void wificonnect(){
Serial.println();
Serial.print("Connecting to ");
Serial.print(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected, IP address: ");
Serial.println(WiFi.localIP());
}
void init_adc_bias() {
for (int i = 0; i < 1 << 8; i++) {
adc_bias += analogRead(A0);
delay(1); // os_delay(1);
}
adc_bias >>= 8;
}
void sampling(){
for (int i = 0; i < 16000; i++) { //2秒サンプリング処理を繰り返す
uint16_t sensorValue = analogRead(A0) - adc_bias; //アナログファイルの読み込み
buffer[i] = sensorValue;//サンプリングファイルの作成
ESP.wdtFeed();
}
}
void makefile(){
SPIFFS.format(); // 書き込み速度のムラをなくす
fd = SPIFFS.open("/sample.wav", "w");
writeRiffHeader(&fd); // ヘッダの書き込み
ESP.wdtDisable();
for (long i = 0; i < 16000; i++) { //2秒サンプリング処理を繰り返す
fd.write((uint8_t*)buffer[i], i);
書き込み
fd.close();
ESP.wdtFeed();
}
}
void writeRiffHeader(Stream fd) {
struct {
char riff[4];
int32_t len1;
char wave[4];
char fmt[4];
int32_t formatSize;
int16_t formatCode;
int16_t channelCount;
int32_t samplingRate;
int32_t bytesPerSecond;
int16_t bytesPerBlock;
int16_t bitsPerSample;
char data[4];
int32_t len2;
} riff_header;
int size = sizeof(riff_header) - 4 + 4 + FREQUENCY * (DURATION / 1000) * 2;
strncpy(riff_header.riff, "RIFF", 4);
riff_header.len1 = size;
strncpy(riff_header.wave, "WAVE", 4);
strncpy(riff_header.fmt, "fmt ", 4); // start of format chunk
riff_header.formatSize = 16;
riff_header.formatCode = 1; // uncompressed PCM
riff_header.channelCount = 1; // monoral
riff_header.samplingRate = FREQUENCY; // Heltz
riff_header.bytesPerSecond = FREQUENCY * 2;
riff_header.bytesPerBlock = 2; // 16bit monoral -> 2byte
riff_header.bitsPerSample = 16; // 16bit
strncpy(riff_header.data, "data", 4); // start of data chunk
riff_header.len2 = FREQUENCY * (DURATION / 1000) * 2; // wave data size
fd->write((uint8_t)&riff_header, sizeof(riff_header));
}
void https_connect() {
HTTPClient client;
String url = HOSTNAME;
client.begin(url);
client.addHeader("Content-Type", "audio/wav");
client.addHeader("Connection", "close");
client.addHeader("AWSAccessKeyId", "<awsaccesskey>");
client.addHeader("key", "sample.wav");
File file = SPIFFS.open("/sample.wav", "r");
long size = file.size();
Serial.println("size is ");
Serial.println(size);
int httpCode = client.sendRequest("POST", &file, size);
Serial.printf("Request for recognize sent.\n", size);
file.close();
if (httpCode == HTTP_CODE_OK) {
payload = client.getString();
Serial.println(payload.c_str());
} else {
Serial.printf("[HTTP] POST... failed, error: %s\n", client.errorToString(httpCode).c_str());
}
client.end();
}
void setup() {
Serial.begin(9600); // シリアル通信の初期化
SPIFFS.begin();
wificonnect();
init_adc_bias(); // ゼロポイントの測定
}
void loop() {
Serial.println("Start sampling");
sampling();
Serial.println("sampling conplete!");
Serial.println("Start make file");
makefile(); //wavファイルの作成
Serial.println("make file conplete!");
Serial.println("Start update to AWS");
https_connect(); //awsへの転送(途中)
Serial.println("update conplete!");
Serial.println();
delay(10000);
}
あなたの回答
tips
プレビュー