2012-11-30 31 views
2

我想能夠使用R.正確的參數下載文件使用Amazon S3 API GET請求

我已經使用被記錄在這裏http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTObjectGET.html

的API開始了我的亞馬遜S3存儲下載 .csv文件

我正在使用包httr來創建GET請求,我只需要確定能夠下載相關文件的正確參數。

我已經設置了response-content-typetext/csv,因爲我知道它.csv文件,我希望能下載...但我得到的迴應如下:

Response [https://s3-zone.amazonaws.com/bucket.name/file.name.csv?response-content-type=text%2Fcsv] 
    Status: 200 
    Content-type: text/csv 
Date and Time,Open,High,Low,Close,Volume 
2007/01/01 22:51:00,5683.00,5683.00,5673.00,5673.00,64 
2007/01/01 22:52:00,5675.00,5676.00,5674.00,5674.00,17 
2007/01/01 22:53:00,5674.00,5674.00,5673.00,5674.00,42 
2007/01/01 22:54:00,5675.00,5676.00,5674.00,5676.00,36 
2007/01/01 22:55:00,5675.00,5676.00,5675.00,5676.00,18 
2007/01/01 22:56:00,5676.00,5677.00,5674.00,5677.00,64 
2007/01/01 22:57:00,5678.00,5678.00,5677.00,5677.00,45 
2007/01/01 22:58:00,5679.00,5680.00,5678.00,5680.00,30 
.../01/01 22:59:00,5679.00,5679.00,5677.00,5678.00,19 

,沒有文件被下載和數據似乎在響應中......我可以提取在響應中創建的代表數據的字符串,我想盡一切努力可以將它轉換爲最初所期望的data.frame,但有沒有更好的方法下載數據...直接從GET命令,然後使用read.csv來讀取數據?我認爲這是一個參數問題......只是不確定需要爲要下載的文件設置哪些參數。

如果有人建議字符串的轉換...這是我有字符串的結構...我需要做些什麼命令才能將它轉換爲data.frame

chr "Date and Time,Open,High,Low,Close,Volume\r\n2007/01/01 22:51:00,5683.00,5683.00,5673.00,5673.00,64\r\n2007/01/01 22:52:00,5675."| __truncated__ 

感謝

HLM

+0

你有代碼,並可能被用於測試可公開訪問的網址是什麼? –

+0

我發現了一個類似的結構。查看直接從'GET'值讀取和使用'colClasses ='提高性能的組合。 –

回答

2

這裏有一種方法:

library(taRifx) # for stack.list 
test <- "Date and Time,Open,High,Low,Close,Volume\r\n2007/01/01 22:51:00,5683.00,5683.00,5673.00,5673.00,64\r\n2007/01/01 22:51:00,5683.00,5683.00,5673.00,5673.00,64\r\n" 
stack(sapply(strsplit(test, "\\n")[[1]], strsplit, split=",")) 

    [,1]     [,2]  [,3]  [,4]  [,5]  [,6]  
ret "Date and Time"  "Open" "High" "Low"  "Close" "Volume\r" 
new "2007/01/01 22:51:00" "5683.00" "5683.00" "5673.00" "5673.00" "64\r"  
new "2007/01/01 22:51:00" "5683.00" "5683.00" "5673.00" "5673.00" "64\r"  

現在轉換成data.frame:

testdat <- stack(sapply(strsplit(test, "\\n")[[1]], strsplit, split=",")) 
rownames(testdat) <- seq(nrow(testdat)) # Because duplicate rownames aren't allowed in data.frames 
colnames(testdat) <- testdat[1,] 
testdat <- testdat[-1,] 
as.data.frame(testdat) 
     Date and Time Open High  Low Close Volume\r 
2 2007/01/01 22:51:00 5683.00 5683.00 5673.00 5673.00  64\r 
3 2007/01/01 22:51:00 5683.00 5683.00 5673.00 5673.00  64\r 
+0

您可以在'「\\ r \\ n」'上進行分割,而不是像示例中那樣返回Windows行尾。 –

+0

嗯,謝謝你,有沒有辦法快速刪除'\ r'位?...但是無論如何這個文件是非常大的> 75MB,所以數據從字符轉換爲data.frame似乎需要很長的時間時間....所以它目前不是理想的解決方案,因爲s3數據已經作爲csv文件上傳了......我仍然希望參數值能夠調整API請求以僅下載數據。 –

+0

在運行之前用'strsplit(test,「\\ r \\ n」)'或者'gsub(「\\ r」,「」test)替換'strsplit(test,「\\ n」)'任何其他代碼。我不確定「只是下載數據」的含義,因爲在我看來,它給了你的是以逗號分隔的形式的數據。 –

2

回答你的第二個問題:

> chr <- "Date and Time,Open,High,Low,Close,Volume\r\n2007/01/01 22:51:00,5683.00,5683.00,5673.00,5673.00,64\r\n" 
> read.csv(text=chr) 
     Date.and.Time Open High Low Close Volume 
1 2007/01/01 22:51:00 5683 5683 5673 5673  64 

如果你想爲read.csv額外的速度,試試這個:

chr <- "Date and Time,Open,High,Low,Close,Volume\r\n2007/01/01 22:51:00,5683.00,5683.00,5673.00,5673.00,64\r\n" 
read.csv(text=chr, colClasses=c("POSIXct", rep("numeric", 5))) 

假設URL設置正確(和我們沒有什麼測試這對還)我想知道如果你可能想看看值GET(...)$content

也許:

infile <- read.csv(text=GET(...)$content, colClasses=c("POSIXct", rep("numeric", 5))) 

編輯:

這是不正確的,因爲數據以「原始」格式顯示。需要將其轉換爲文本之前,從原始轉換。我做了一個Nabble的快速搜索(它對所有事情來說都是很好的),以找到一個駐留在Web上的csv文件。這是最後的工作:

read.csv(text=rawToChar( 
       GET(
        "http://nseindia.com/content/equities/scripvol/datafiles/16-11-2012-TO-16-11-2012ACCEQN.csv" 
        )[["content"]])) 
    Symbol Series  Date Prev.Close Open.Price High.Price Low.Price Last.Price Close.Price 
1 ACC  EQ 16-Nov-2012  1404.4 1410.95 1410.95 1369.45 1374.95  1378.1 
    Average.Price Total.Traded.Quantity Turnover.in.Lacs Deliverable.Qty X..Dly.Qt.to.Traded.Qty 
1  1393.62    132921   1852.41   56899     42.81 
+0

比我試圖重新發明方向盤更好。也可能更快。 –

+0

這個簡單的解決方案實際上工作得很好......但現在最大的滯留似乎是在對字符串的響應轉換中......使用'content' ...我目前使用的代碼是'x < - GET(end.point,add_headers(Date = time.string,Authorization = authorization.string),query = params)' \t'y < - read.csv(text = content(x))' 任何想法關於如何加快速度? –

+0

我的猜測是服務器響應比read.csv步驟慢很多。你用'system.time'分析過它嗎? (另外:使用colClasses可以加快所有'read。*'函數的速度。) –