node.js调用.c文件_在Node.js中分派S3文件
node.js調用.c文件
Some of our intranet backends use S3 storage and GraphQL APIs. It’s a common scenario nowadays. This story is about how we deal with file attachments in our schemas and how our client code can get hold of the real files.
我們的某些Intranet后端使用S3存儲和GraphQL API。 如今,這是一種常見的情況。 這個故事是關于我們如何處理模式中的文件附件以及我們的客戶端代碼如何獲取實際文件。
具有基于JWT令牌的身份驗證的簡單GraphQL服務器 (A simple GraphQL server with JWT token based authentication)
Let’s start with a very simple server:
讓我們從一個非常簡單的服務器開始:
We’ll use Koa to build the http server and apollo-server-koa to integrate an Apollo GraphQL server with Koa.
我們將使用Koa構建http服務器,并使用apollo-server-koa將Apollo GraphQL服務器與Koa集成。
The GraphQL schema allows querying for the files of the logged user.
GraphQL模式允許查詢已登錄用戶的文件。
It uses jsonwebtoken to authenticate users using JWT tokens. For the sake of simplicity it uses a password instead of a digital certificate to sign the tokens. We also assume that those tokens are generated elsewhere. The token payload always contains the user login.
它使用jsonwebtoken使用JWT令牌對用戶進行身份驗證。 為了簡單起見,它使用密碼而不是數字證書來對令牌進行簽名。 我們還假設這些令牌是在其他位置生成的。 令牌有效負載始終包含用戶登錄名。
To resolve the files we ask our S3 endpoint for all the entries in a test Bucket that match the following “path” files/<login>/**. We use the listObjectsV2 function from the Amazon aws-sdk package.
為了解析文件,我們向S3端點詢問test 桶中與以下“路徑” files/<login>/**相匹配的所有條目。 我們使用Amazon aws-sdk包中的listObjectsV2函數。
If you don’t have access to an S3 account or you prefer to test all this stuff locally, you can use the fabulous minio. You can easily set up a local instance using docker and expose some folder through an S3 API compatible endpoint.
如果您無權使用S3帳戶,或者希望在本地測試所有這些東西,則可以使用神話般的minio 。 您可以使用docker輕松設置本地實例,并通過與S3 API兼容的端點公開某些文件夾。
I’m not digging further into any of these concepts. Each of them deserves a story of its own.
我不會進一步研究這些概念。 他們每個人都有自己的故事。
Below you can see the tiniest implementation I can think of for this server:
在下面,您可以看到該服務器可以想到的最微小的實現:
With this in place we can make a GraphQL query in the playground at http://localhost:3000/graphql and get all the files.
有了這個,我們可以在操場上通過http:// localhost:3000 / graphql進行GraphQL查詢,并獲取所有文件。
Notice that, as I said before, you’ll need to get a token and set the Authorization header in the playground. You can use the following script to create tokens for testing purposes: new-api-token user1.
請注意,正如我之前所說,您將需要獲取令牌并在操場上設置Authorization標頭。 您可以使用以下腳本來創建令牌以進行測試: new-api-token user1 。
Our GraphQL ouput is not very useful yet. The url fields don’t contain real endpoints. Let’s fix that.
我們的GraphQL輸出不是很有用。 url字段不包含真實的端點。 讓我們修復它。
獲取真實文件 (Getting the real files)
We’ll change the resolver return statement to return a collection of S3 signed urls using the getSignedUrlPromise method. A signed url points to the S3 storage server and includes a temporary access token to control how and when a client can access a resource.
我們將更改解析程序的return語句,以使用getSignedUrlPromise方法返回S3簽名的url的集合 。 簽名的url指向S3存儲服務器,并包含一個臨時訪問令牌,以控制客戶端如何以及何時訪問資源。
Now we can visit the url in the url field returned by the GraphQL and download the file attachment.
現在,我們可以在GraphQL返回的url字段中訪問url并下載文件附件。
This approach has several problems though:
但是,這種方法有幾個問題:
We need to orchestrate and wait for several async calls to getSignedUrlPromise. Thus having a performance penalty (n+1 operations).
我們需要編排并等待對getSignedUrlPromise幾個異步調用。 因此具有性能損失(n + 1次操作)。
- We don’t really know if the client is going to really request any of the files, so, preloading the signed urls could be a waste of time. 我們真的不知道客戶端是否會真正請求任何文件,因此,預加載簽名的URL可能會浪費時間。
- The signed urls have expiration times (60 seconds in the example). We could make them non expirable but for security reasons we may don’t want to. Preloading all the urls could make them stale before the client requests the files. 簽名的URL具有有效時間(在示例中為60秒)。 我們可以使它們不過期,但出于安全原因,我們可能不希望這樣做。 在客戶端請求文件之前,預加載所有URL可能會使它們過時。
To solve this problems we are going to dispatch the files on demand.
為了解決這個問題,我們將按需分發文件。
調度文件 (Dispatching files)
Let’s start by changing our resolver again to make the urls point to a new dispatch REST endpoint.
讓我們從再次更改解析器開始,以使url指向新的調度REST端點。
Now we need to code the endpoint. We could do two things:
現在我們需要對端點進行編碼。 我們可以做兩件事:
- Read the data from the s3 bucket and stream it to the client. 從s3存儲桶中讀取數據,并將其流式傳輸到客戶端。
- Generate a signed url and redirect the client. 生成簽名的URL并重定向客戶端。
Let’s go with the second option. There’s no need to do ourselves what the S3 endpoint can do for us.
讓我們來看第二個選項。 無需自己做S3端點可以為我們做的事情。
We need to add the REST endpoint before we setup the GraphQL server to intercept the requests before they reach the Apollo middleware. The dispatcher route matches any Bucket and Bucket Key. It authenticates the user via the JWT token and then generates the signed url and redirects. The token must be added as a token url param to the dispatch url by the client.
在設置GraphQL服務器以攔截請求到達Apollo中間件之前,我們需要添加REST端點。 調度程序路由匹配任何存儲桶和存儲桶密鑰。 它通過JWT令牌對用戶進行身份驗證,然后生成簽名的url并重定向。 客戶端必須將令牌作為token URL參數添加到調度URL。
There’s a missing piece though. We are just checking that the token is valid. We are not doing any authorization at all. Using a user2 valid token we can access user1 files 😱.
不過有一塊失蹤的東西。 我們只是在檢查令牌是否有效。 我們根本沒有做任何授權。 使用一個user2有效令牌,我們可以訪問user1文件😱。
改善安全性 (Improving security)
Fear not. We can add authorization policies to our dispatcher.
不要怕。 我們可以向我們的調度員添加授權策略。
Given that the dispatcher is pretty generic and matches any Bucket and Key, we can implement a regitry of checkers. Each checker must say if it must be applied to a request (the matcher function) and check the user permissions (the check function).
鑒于調度程序非常通用,并且與任何Bucket和Key匹配,因此我們可以實現檢查程序的功能。 每個檢查者必須說出是否必須將其應用于請求( matcher功能),并檢查用戶權限( check功能)。
We’ll traverse this registry to find the appropriate check for each request. As our last check we’ll have a default one that forbids any access.
我們將遍歷此注冊表以找到每個請求的適當檢查。 作為我們的最后檢查,我們將使用默認值禁止任何訪問。
We add this logic to the dispatcher swapping lines 8–9 with the following:
我們將此邏輯添加到調度程序的第8–9行交換中,內容如下:
And that’s it for this story: dispatching s3 files on demand with JWT based authentication/authorization. You can check the full server in all its big-one-file glory in this gist.
就是這樣:通過基于JWT的身份驗證/授權按需分發s3文件。 您可以在本要點中查看完整服務器的所有大文件大榮耀。
加起來 (Summing up)
When dealing with S3 file references in a GraphQL schema it’s better to use an intermediate dispatcher to generate S3 signed url on demand and add authorization policies. This logic also applies to other kind of APIs, not only to GraphQL.
在GraphQL模式中處理S3文件引用時,最好使用中間調度程序來按需生成S3簽名的url并添加授權策略。 此邏輯也適用于其他類型的API,不僅適用于GraphQL。
In this story we’ve used a very straightforward implementation with coarse and naive error checking, and tons of hardcoded stuff. To put this in production will require more finesse 😅. I think it will still help you to grasp the concept.
在這個故事中,我們使用了非常簡單的實現,包括粗略的天真錯誤檢查以及大量的硬編碼內容。 將其投入生產將需要更多技巧。 我認為它仍然可以幫助您掌握概念。
It’s quite easy to do all this stuff in node with npm packages like koa, apollo-server-koa, jsonwebtoken and aws-sdk.
使用npm包(例如koa , apollo-server-koa , jsonwebtoken和aws-sdk在節點中完成所有這些工作非常容易。
And yes, I think so too. The the aws-sdk fixation on PascalCase parameter names is obnoxious 😑.
是的,我也這樣認為。 PascalCase參數名稱的aws-sdk固定令人討厭。
翻譯自: https://medium.com/trabe/dispatching-s3-files-in-node-js-a0bc0ad6e8c9
node.js調用.c文件
總結
以上是生活随笔為你收集整理的node.js调用.c文件_在Node.js中分派S3文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux重置网络的命令,Linux常用
- 下一篇: Simditor引入注意事项以及修改接入