object.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. const debug = require('debug')('ali-oss:object');
  2. const fs = require('fs');
  3. const is = require('is-type-of');
  4. const copy = require('copy-to');
  5. const path = require('path');
  6. const mime = require('mime');
  7. const callback = require('./common/callback');
  8. const { Transform } = require('stream');
  9. const pump = require('pump');
  10. const { isBuffer } = require('./common/utils/isBuffer');
  11. const proto = exports;
  12. /**
  13. * Object operations
  14. */
  15. /**
  16. * append an object from String(file path)/Buffer/ReadableStream
  17. * @param {String} name the object key
  18. * @param {Mixed} file String(file path)/Buffer/ReadableStream
  19. * @param {Object} options
  20. * @return {Object}
  21. */
  22. proto.append = async function append(name, file, options) {
  23. options = options || {};
  24. if (options.position === undefined) options.position = '0';
  25. options.subres = {
  26. append: '',
  27. position: options.position
  28. };
  29. options.method = 'POST';
  30. const result = await this.put(name, file, options);
  31. result.nextAppendPosition = result.res.headers['x-oss-next-append-position'];
  32. return result;
  33. };
  34. /**
  35. * put an object from String(file path)/Buffer/ReadableStream
  36. * @param {String} name the object key
  37. * @param {Mixed} file String(file path)/Buffer/ReadableStream
  38. * @param {Object} options
  39. * {Object} options.callback The callback parameter is composed of a JSON string encoded in Base64
  40. * {String} options.callback.url the OSS sends a callback request to this URL
  41. * {String} options.callback.host The host header value for initiating callback requests
  42. * {String} options.callback.body The value of the request body when a callback is initiated
  43. * {String} options.callback.contentType The Content-Type of the callback requests initiatiated
  44. * {Object} options.callback.customValue Custom parameters are a map of key-values, e.g:
  45. * customValue = {
  46. * key1: 'value1',
  47. * key2: 'value2'
  48. * }
  49. * @return {Object}
  50. */
  51. proto.put = async function put(name, file, options) {
  52. let content;
  53. options = options || {};
  54. name = this._objectName(name);
  55. if (isBuffer(file)) {
  56. content = file;
  57. } else if (is.string(file)) {
  58. const stats = fs.statSync(file);
  59. if (!stats.isFile()) {
  60. throw new Error(`${file} is not file`);
  61. }
  62. options.mime = options.mime || mime.getType(path.extname(file));
  63. const stream = fs.createReadStream(file);
  64. options.contentLength = await this._getFileSize(file);
  65. return await this.putStream(name, stream, options);
  66. } else if (is.readableStream(file)) {
  67. return await this.putStream(name, file, options);
  68. } else {
  69. throw new TypeError('Must provide String/Buffer/ReadableStream for put.');
  70. }
  71. options.headers = options.headers || {};
  72. this._convertMetaToHeaders(options.meta, options.headers);
  73. const method = options.method || 'PUT';
  74. const params = this._objectRequestParams(method, name, options);
  75. callback.encodeCallback(params, options);
  76. params.mime = options.mime;
  77. params.content = content;
  78. params.successStatuses = [200];
  79. const result = await this.request(params);
  80. const ret = {
  81. name,
  82. url: this._objectUrl(name),
  83. res: result.res
  84. };
  85. if (params.headers && params.headers['x-oss-callback']) {
  86. ret.data = JSON.parse(result.data.toString());
  87. }
  88. return ret;
  89. };
  90. /**
  91. * put an object from ReadableStream. If `options.contentLength` is
  92. * not provided, chunked encoding is used.
  93. * @param {String} name the object key
  94. * @param {Readable} stream the ReadableStream
  95. * @param {Object} options
  96. * @return {Object}
  97. */
  98. proto.putStream = async function putStream(name, stream, options) {
  99. options = options || {};
  100. options.headers = options.headers || {};
  101. name = this._objectName(name);
  102. if (options.contentLength) {
  103. options.headers['Content-Length'] = options.contentLength;
  104. } else {
  105. options.headers['Transfer-Encoding'] = 'chunked';
  106. }
  107. this._convertMetaToHeaders(options.meta, options.headers);
  108. const method = options.method || 'PUT';
  109. const params = this._objectRequestParams(method, name, options);
  110. callback.encodeCallback(params, options);
  111. params.mime = options.mime;
  112. const transform = new Transform();
  113. // must remove http stream header for signature
  114. transform._transform = function _transform(chunk, encoding, done) {
  115. this.push(chunk);
  116. done();
  117. };
  118. params.stream = pump(stream, transform);
  119. params.successStatuses = [200];
  120. const result = await this.request(params);
  121. const ret = {
  122. name,
  123. url: this._objectUrl(name),
  124. res: result.res
  125. };
  126. if (params.headers && params.headers['x-oss-callback']) {
  127. ret.data = JSON.parse(result.data.toString());
  128. }
  129. return ret;
  130. };
  131. proto.getStream = async function getStream(name, options) {
  132. options = options || {};
  133. if (options.process) {
  134. options.subres = options.subres || {};
  135. options.subres['x-oss-process'] = options.process;
  136. }
  137. const params = this._objectRequestParams('GET', name, options);
  138. params.customResponse = true;
  139. params.successStatuses = [200, 206, 304];
  140. const result = await this.request(params);
  141. return {
  142. stream: result.res,
  143. res: {
  144. status: result.status,
  145. headers: result.headers
  146. }
  147. };
  148. };
  149. proto.putMeta = async function putMeta(name, meta, options) {
  150. return await this.copy(name, name, {
  151. meta: meta || {},
  152. timeout: options && options.timeout,
  153. ctx: options && options.ctx
  154. });
  155. };
  156. proto.list = async function list(query, options) {
  157. // prefix, marker, max-keys, delimiter
  158. const params = this._objectRequestParams('GET', '', options);
  159. params.query = query;
  160. params.xmlResponse = true;
  161. params.successStatuses = [200];
  162. const result = await this.request(params);
  163. let objects = result.data.Contents;
  164. const that = this;
  165. if (objects) {
  166. if (!Array.isArray(objects)) {
  167. objects = [objects];
  168. }
  169. objects = objects.map(obj => ({
  170. name: obj.Key,
  171. url: that._objectUrl(obj.Key),
  172. lastModified: obj.LastModified,
  173. etag: obj.ETag,
  174. type: obj.Type,
  175. size: Number(obj.Size),
  176. storageClass: obj.StorageClass,
  177. owner: {
  178. id: obj.Owner.ID,
  179. displayName: obj.Owner.DisplayName
  180. }
  181. }));
  182. }
  183. let prefixes = result.data.CommonPrefixes || null;
  184. if (prefixes) {
  185. if (!Array.isArray(prefixes)) {
  186. prefixes = [prefixes];
  187. }
  188. prefixes = prefixes.map(item => item.Prefix);
  189. }
  190. return {
  191. res: result.res,
  192. objects,
  193. prefixes,
  194. nextMarker: result.data.NextMarker || null,
  195. isTruncated: result.data.IsTruncated === 'true'
  196. };
  197. };
  198. proto.listV2 = async function listV2(query, options = {}) {
  199. const continuation_token = query['continuation-token'] || query.continuationToken;
  200. delete query['continuation-token'];
  201. delete query.continuationToken;
  202. if (continuation_token) {
  203. options.subres = Object.assign(
  204. {
  205. 'continuation-token': continuation_token
  206. },
  207. options.subres
  208. );
  209. }
  210. const params = this._objectRequestParams('GET', '', options);
  211. params.query = Object.assign({ 'list-type': 2 }, query);
  212. params.xmlResponse = true;
  213. params.successStatuses = [200];
  214. const result = await this.request(params);
  215. let objects = result.data.Contents;
  216. const that = this;
  217. if (objects) {
  218. if (!Array.isArray(objects)) {
  219. objects = [objects];
  220. }
  221. objects = objects.map(obj => ({
  222. name: obj.Key,
  223. url: that._objectUrl(obj.Key),
  224. lastModified: obj.LastModified,
  225. etag: obj.ETag,
  226. type: obj.Type,
  227. size: Number(obj.Size),
  228. storageClass: obj.StorageClass,
  229. owner: obj.Owner
  230. ? {
  231. id: obj.Owner.ID,
  232. displayName: obj.Owner.DisplayName
  233. }
  234. : null
  235. }));
  236. }
  237. let prefixes = result.data.CommonPrefixes || null;
  238. if (prefixes) {
  239. if (!Array.isArray(prefixes)) {
  240. prefixes = [prefixes];
  241. }
  242. prefixes = prefixes.map(item => item.Prefix);
  243. }
  244. return {
  245. res: result.res,
  246. objects,
  247. prefixes,
  248. isTruncated: result.data.IsTruncated === 'true',
  249. keyCount: +result.data.KeyCount,
  250. continuationToken: result.data.ContinuationToken || null,
  251. nextContinuationToken: result.data.NextContinuationToken || null
  252. };
  253. };
  254. /**
  255. * Restore Object
  256. * @param {String} name the object key
  257. * @param {Object} options
  258. * @returns {{res}}
  259. */
  260. proto.restore = async function restore(name, options) {
  261. options = options || {};
  262. options.subres = Object.assign({ restore: '' }, options.subres);
  263. if (options.versionId) {
  264. options.subres.versionId = options.versionId;
  265. }
  266. const params = this._objectRequestParams('POST', name, options);
  267. params.successStatuses = [202];
  268. const result = await this.request(params);
  269. return {
  270. res: result.res
  271. };
  272. };
  273. proto._objectUrl = function _objectUrl(name) {
  274. return this._getReqUrl({ bucket: this.options.bucket, object: name });
  275. };
  276. /**
  277. * generator request params
  278. * @return {Object} params
  279. *
  280. * @api private
  281. */
  282. proto._objectRequestParams = function (method, name, options) {
  283. if (!this.options.bucket && !this.options.cname) {
  284. throw new Error('Please create a bucket first');
  285. }
  286. options = options || {};
  287. name = this._objectName(name);
  288. const params = {
  289. object: name,
  290. bucket: this.options.bucket,
  291. method,
  292. subres: options && options.subres,
  293. timeout: options && options.timeout,
  294. ctx: options && options.ctx
  295. };
  296. if (options.headers) {
  297. params.headers = {};
  298. copy(options.headers).to(params.headers);
  299. }
  300. return params;
  301. };
  302. proto._objectName = function (name) {
  303. return name.replace(/^\/+/, '');
  304. };
  305. proto._statFile = function (filepath) {
  306. return new Promise((resolve, reject) => {
  307. fs.stat(filepath, (err, stats) => {
  308. if (err) {
  309. reject(err);
  310. } else {
  311. resolve(stats);
  312. }
  313. });
  314. });
  315. };
  316. proto._convertMetaToHeaders = function (meta, headers) {
  317. if (!meta) {
  318. return;
  319. }
  320. Object.keys(meta).forEach(k => {
  321. headers[`x-oss-meta-${k}`] = meta[k];
  322. });
  323. };
  324. proto._deleteFileSafe = function (filepath) {
  325. return new Promise(resolve => {
  326. fs.exists(filepath, exists => {
  327. if (!exists) {
  328. resolve();
  329. } else {
  330. fs.unlink(filepath, err => {
  331. if (err) {
  332. debug('unlink %j error: %s', filepath, err);
  333. }
  334. resolve();
  335. });
  336. }
  337. });
  338. });
  339. };